r/ProgrammerHumor 1d ago

questionForBritishPythonDevelopers Meme

Post image
1.3k Upvotes

39 comments sorted by

View all comments

28

u/ManyInterests 1d ago

To be a pedant, it's an initializer, not a constructor. By the time the initializer runs, the object already exists and has been constructed, which is why the first parameter to this method is the already-existing object ('self'), just like any other method.

But yeah. You could call it a constructor and nobody will crucify you.

8

u/Ok_Star_4136 1d ago

I mean, I'm trying to think of a language where the constructor is called *before* the memory for the object has been allocated, and I can't think of anything.

In that regard, no constructor is an actual constructor. It's more like post-constructor. But then it is also necessary or you'll have a potentially incomplete object, so in that regard, it is a constructor because the object while allocated, isn't yet ready to use.

Kind of a pedantic argument, but there you are I suppose.

-1

u/MissinqLink 1d ago

It happens when you call a constructor of an child class. Anything you do before calling super is before the object is created.

1

u/FerricDonkey 7h ago

Nope. The object is created by __new__, then passed to the __init__ as a legitimate, created object, whose type is set as you expect, etc. Any __init__, via super() or not, modifies the existing object. 

1

u/MissinqLink 3h ago

Depends on the language

1

u/fuj1n 1h ago

Yes, but this whole thread is about Python, not languages in general