Constructors in Python
Python’s class constructor is a fundamental part of OOP. They allow you to fully initialize objects of a given Class and makes them ready-to-use.
But how do they do that?
Constructors internally trigger Python’s instantiation process — which basically is instance creation and then instance initialization.
Today I am going to try and teach you everything that you need to know to properly use constructors in your future programs.
So once you have decided what your class will look like — you can start creating instances and objects. The process of creating objects is called object instantiation, and the tool responsible for running this instantiation process is commonly known as a class constructor.
In order to construct an object of a given class in Python, you just have to call the class with proper arguments and assign it to a variable(This variable will become the so called object of your class)
In the above example we define a class called Policeman. This is our “blueprint” for creating more policemen. In our case, when we call the Policeman class we’re calling the class constructor, which creates, initializes, and returns a new object by triggering Python’s internal instantiation process. All of the OOP languages have similar instantiating process. The important thing here is to understand the logic.
Besides instantiating the class — we give it some data. Or in our case we assign the Policeman his most trusted friend — the pistol. Then we create a method to print what weapon is he carrying.
As you can imagine — the output of our program simply prints “Pistol”.
But this is pretty boring right? Lets add some more cool stuff to our code.
This looks much better. Again you can see that we are “constructing” new Policeman objects with the same procedure as before. This time, we are supplying the correct number of arguments. The output is as follows:
Mainly , there are 2 types of constructors. Default constructor and parameterized constructor. The default constructor is pretty self explanatory — it is a constructor without additional arguments supplied. The only argument that it has is the one which is a reference to the instance being constructed. (“self”)
A parameterized constructor, also referred to as a constructor with parameters, is one that accepts parameters during its initialization. It begins with a reference to the instance being created, typically denoted as ‘self’, and the subsequent arguments are supplied by the programmer.
Keep in mind that .__init__()
must not return anything different from None
, or you’ll get a TypeError
exception.
I hope that this is enough to get you started on the topic of constructors. Essentially constructors give you the ability to define a special kind of function that makes construction of new fully initialized objects easier to work with , and on top of that it makes you less likely to encounter errors while developing your programs. Not to mention that, constructors can be used to enforce encapsulation, by ensuring that the object’s attributes are initialized correctly and in a controlled manner. But this is topic for another tutorial.
Some issues or downsides with Python constructors is that they do not support overloading. This means that you cannot have multiple constructors with different parameters in a given class.
Thanks for reading ! See you in the next ones !