“What function is a constructor in Python?” —I was asked this question several times during my job interviews for Python developer. At first, I’ve been answering that it’s simply __init__, then a friend of mine explained to me, that of course it’s __new__. I’ve been living with this knowledge peacefully ever since, until recently I got a phonecall from a former colleague, telling me that some guy at the job interview laughed at him for saying that __new__ is a correct answer for this question. To him, __init__ was obviously the right answer.

This caused me to stop and think about it for a minute. Did I acctualy read about this issue in the documentation? Well, not quite. I just assumed while learning Python that it was __init__ and then I’ve been told that it’s actually __new__. There shouldn’t be any confusion on this topic, right? Let’s just check Python’s documentation and solve this problem once and for all.

What is a constructor

First, we’ve got to define what are we looking for. So according to Wikipedia:

And what is a subroutine?

In computer programming, a subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.[…] — Wikipedia

So a subroutine sounds just like a function, right? So what’s the difference between function and subroutine, exactly? It seems, that it is… disputable. I’ve found a large amount of definitions, some of which assume that both function and subroutine are the same thing (thefreedictionary actually redirects function searches to subroutine). Other definition states that function is a type of routine and is named. I would say, that according to internet resources two terms can be used interchangably.

We’ve got a subroutine exaplained, so coming back to the Wikipedia’s definition of a constructor: “[…] type of subroutine called to create an object”. Well, first of all, there is no statement that this subroutine has to be responsible for creating new object. It’s called to create an object — the creation part seems to be delegatable.

Since we’ve pretty much exhausted the definition of constructor, let’s look at Python docs.

What do the docs say?

Surprisingly enough, Python docs have very little to say about what is actually a constructor method. The word “constructor” appears only in connection to the function with the same name as the class which object we’re trying to create. So some of the constructors mentioned in docs are: dict(), set(), bytes() etc. Let’s take a different path, then. Let’s check __new__ method’s definition:

Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument. The remaining arguments are those passed to the object constructor expression (the call to the class). The return value of __new__() should be the new object instance (usually an instance of cls). — Python docs

Ok, that gives us a lot of information. So there is syntactic element in Python that is called “constructor expression” and it’s basically a call to the class. But in different place it’s just called a constructor. Let’s check definition of __init__ also:

Called after the instance has been created (by __new__() ), but before it is returned to the caller. The arguments are those passed to the class constructor expression. […]. — Python docs

Constructor expression strikes again. It looks like constructor and constructor expression are exactly the same thing. Let’s read on:

Because __new__() and __init__() work together in constructing objects ( __new__() to create it, and __init__() to customize it), no non- None value may be returned by __init__() ; doing so will cause a TypeError to be raised at runtime. — Python docs

Now we’re onto something. "__new__() and __init__() work together in constructing objects” sounds a lot like they are both parts of a constructor. So neither of them is a standalone constructor method. There is no such thing as a standalone constructor function in Python language. There is a constructor expression, which calls __new__ and __init__ in sequence. Case closed.

Why confusion?

This is the beginning of my own speculation on the reasons of this misunderstanding, so if you’re interested only in facts, you can stop right here.

If there is a call, we are naturally looking for a function that’s being called. So if we try to create an object using constructor expression, there must be a constructor function sitting somewhere waiting to be called. When it turns out that there is no such function we still look for something that would be just like it, even if it does not meet the requirements that we imposed on it in the first place (e.g. name identical with the class name, which I think most people would attribute to a constructor function). So it does not have the same name, but it must be somewhere, right?

Well, in this case it simply isn’t. That’s because, while we cheerfully search for thing that must be there, we skip really important part of Python language. Namely, in Python everything is an object. Functions are objects. And classes are too. If we think about it for a second, it turns out that calling a function in Python is not that different from calling a class.

Let’s try with an example. Let’s define a function and a class:

def func(param):

return param class Class:

def __init__(self, a):

self.a = a

Now let’s check the insides of those two:

>>> dir(func)

['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

>>>

>>> dir(Class)

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Ok, so the inside methods are different, but both of them look just like regular objects. The thing that I want to direct your attention to is a method __call__ of the func function. What does it actually do?

>>> func.__call__(1)

1

>>> func(1)

1

That was a trick question — it is called when the function is called. It does look like something, that we’ve examined when talking about constructors, though… The object (in this case it’s a function object) is called, and the result is that one of it’s internal methods is called. The exact same thing happens when the class object is called, but instead of __call__, method’s name is __new__. It creates new object, and every new object’s __init__ method is called instantly after it’s creation. And just like <function_name>() can be called a function expression, <class_name>() can be called a constructor expression, or simply a constructor.

Final remarks

At first this topic did not seem to be really interesting to me, but when I invested some more time into it, it turned out that there was more to it. I invite everyone to a discussion and to fact check this issue.