What is a constructor?

In the code below, some people say init is not a constructor but when I look at online sources many articles say the init is a constructor. It’s confusing cause I thought the init was a method.

Can someone please clarify what an init actually is and what is a constructor? In one of the sections (classes) it states we don’t have to use the init method all the time, why is that and when would it be appropriate to use it?

class Employee: def __init__(self, id,name): self.id=id self.name=name def display(self): print("Hello my id is :", self.id) print("My name is :", self.name) e1=Employee(1, 'Nithin') e1.display() e2=Employee(2, 'Arjun') e2.display()

__init__() is basically a method that is called when an instance of the class is created. When you initialise a class, and pass in arguments, like you have here:

e1=Employee(1, 'Nithin')

The __init__() method is called, and each of the arguments is dealt with. In your example, each argument is assigned to a particular instance variable (for example, self.id).

What are instance variables

An instance variable means that the variable holds a different value for every instance of the class. In your case, e1.id would be 1, whereas e2.id would be 2.

So the __init__() method essentially constructs an object with the properties given to it, in the template of the class you’re creating an instance of.

An analogy

If you think of a chair from IKEA. It’s part of the “chair” class. Each chair is an instance of the “chair” class. Each part of the chair is an “instance variable” of the “chair” class. When the chair was created, various properties (instructions, if you will) were given to whoever made it. That person is like the __init__() method. They take a bunch of arguments/instructions, and make an instance of the “chair” class with them. They don’t do anything with the chair, they just make it. The rest of the class’ methods (like sit(), perhaps, or even break()) are implemented later.

Here’s a good article that further explains it.

3 Likes

No matter where you fall on term constructor, __init__ is still a method. As for the constructor part I think the issue is that __init__ itself doesn’t create the object so many think it does not meet the criteria to be called a constructor and should instead be called an initialiser or otherwise.

Another issue is that term constructor seems to vary a little between languages, in some the constructor is basically responsible for two operations, creating a new object and preparing/initialising a new object, __init__ only does one of these. Object creation in Python is technically done via .__new__ from the metaclass (which is normally type) but you’ll almost never have to override that method (there are few reasons to do so).

However, in actual use the term constructor seems to flip-flop around a little (it seems to be mostly a language based thing as the behaviour and implementation differs between languages). It is not uncommon to find many well-established devs calling __init__ a constructor and even the docs seem to dance around the term. So as far as terminology goes you’ll have to make your own mind up or find a better resource then me :rofl: to be your gold standard.

As far as Python goes just be aware that object creation and object initialisation are technically different tasks, performed by different methods. It is valid to create objects without using __init__. It is also allowed, if a little uncommon to call __init__ again after the first initialisation (perhaps to reset or modify some data) and in that way it behaves more like a normal method.

3 Likes

Thank you, this did help clear things up for me. I am new to programming and the last section of classes seems to involve more information and focus compared to some of the other sections IMO.

But I did see an article that says a constructor is just a task that the computer does to help when initializing an object.

1 Like

Yeah, if you find a resource you trust then feel free to follow their convention. Just be aware that some folks will use it in slightly different ways and the meaning may wobble a little depending on language. Wikipedia has a nice overview of how it may be used in several different languages but that doesn’t make it an official definition (it does highlight why confusion may pop up since there are slightly different meanings and implementations for each language).

At the end of the day you might need to rely on context to know exactly what any one person means when they say constructor, so long as you can infer the meaning that’s all you can hope for (or at least that’s how I see it :laughing:).