This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here
- 30 Days of Python 👨💻 - Day One - Introduction
- 30 Days Of Python 👨💻 - Day 2 - Data Types I
- 30 Days of Python 👨💻 - Day 3 - Data Types II
- 30 Days Of Python 👨💻 - Day 4 - Data Types III
- 30 Days Of Python 👨💻 - Day 5 - Conditions And Loops I
- 30 Days Of Python 👨💻 - Day 6 - Loops II And Functions
- 30 Days Of Python 👨💻 - Day 7 - Developer Environment
Python is a multi-paradigm language. Coming from the JavaScript universe, I am aware of this as JavaScript is a multi-paradigm language as well.
What it means is there is more than one explicit way of thinking about how we write our code in Python, and structure our code. Now, why is this important? In the real-world, while working on real-life projects, the problems we try to solve with programming are complex and involve a lot of brainstorming even before writing a single line of code. Good programmers not only think about how to solve the problem with code but how to write code that is easier to maintain, easier to extend when necessary, and also easier to read and write. This way of structuring and organizing code is known as a programming paradigm. It’s like a pattern with some predefined set of rules which the developers can follow to avoid chaos. Imagine if every developer tried to be shrewd and wrote code in their unique way. Without a definite pattern, the project would be doomed!
Back to Python!
In Python, everything is an Object. The data types that I explored are all objects which have their own associated attributes and methods to do some action. These objects come from their classes as an instance. It means that all the data types in Python have a defined structure or prototype where all the details of their properties and functionalities have been defined.
- print(type(2)) # <class 'int'>
- print(type(2.5)) # <class 'float'>
- print(type('Python')) # <class 'str'>
- print(type(True)) # <class 'bool'>
- print(type({})) # <class 'dict'>
- print(type([])) # <class 'list'>
- print(type(())) # <class 'tuple'>
- print(type(None)) # <class 'NoneType'>
Just like the built-in classes, custom classes can be created to represent real-world things like Cars, Machines, Human Being, Animal or anything. This representation of real-world entities and their properties and behaviors into classes in code is can be thought of as a loose definition of Object Oriented Programming paradigm. Each class can then be used to created instances of an object. These objects can be combined with other objects to simulate real-world functionalities.
In the JavaScript universe as well, custom classes can be created (although classes in JS are more of syntactic sugar on top of prototype functions introduced with ES6). So in my mental model, I hypothetically connected them.
But to see OOP in action in Python will have to dive deep and write some code.
- class Avenger: def __init__(self, name): self.name = name def fight(self): print('👊') spiderman = Avenger('Spiderman') print(type(Avenger)) # <class 'type'>
- print(type(spiderman)) # <class '__main__.Avenger'> --> instance of Avenger
In Python, the naming convention for classes is camel-case and singular names, unlike variables which need to be snake-cased.
The __init__ is an initialization method (also called the constructor method). It is used to initialize the variables of the class. In the above class, name is being initialized. In JavaScript, for instance, this is done similarly in the constructor function of the class.
self is a keyword in Python which is a reference to the instance of the class. It is used to access the variables or attributes of the class. In my mental model, I compare this with the JavaScript this keyword.


Join the conversation! Your thoughts help the community grow.