loading...

Constructors in Python (__init vs __new__)

pila profile image Pila louis ・2 min read

Most object-oriented programming languages such as Java, C++, C#..etc have the concept of a constructor, a special method that creates and initializes the object when it is created. Python is a little different; it has a constructor and an initializer. The constructor function is rarely used unless you're doing something exotic. So, we'll start our discussion with the initialization method. 

The assumption in this article is that you already know the basics of classes and objects in python. 

The constructor function in python is called __new__ and __init__ is the initializer function.

Quoting the python documentation, __new__ is used when you need to control the creation of a new instance while __init__ is used when you need to control the initialization of a new instance.

__new__ is the first step of instance creation. It's called first and is responsible for returning a new instance of your class.

In contrast, __init__ doesn't return anything; it's only responsible for initializing the instance after it's been created. In general, you shouldn't need to override __new__unless you're subclassing an immutable type like str, int, Unicode, or tuple.

NOTE: 
Never name a function of your own with leading and trailing double underscores. It may mean nothing to Python, but there's always the possibility that the designers of Python will add a function that has a special purpose with that name in the future, and when they do, your code will break.

Example 1: Using __init__

class Point:

    def __init__(self, data):
        self.num = data

    def print_num(self):
        print(self.num)



obj = Point(100)

obj.print_num()

Output:

100

Note: The self parameter is a reference to the current instance of the class and is used to access variables that belong to the class.

Example 2:

class Person:

    def __new__(cls):
        return object.__new__(cls)

    def __init__(self):
        self.instance_method()

    def instance_method(self):
        print('success!')

personObj = Person()

Notice that __init__ receives the argument self, while __new__ receives the class (cls). Since self is a reference to the instance, this should tell you quite evidently that the instance is already created by the time __init__ gets called, since it gets passed the instance. It's also possible to call instance methods precisely because the instance has already been created.

Thank you for reading. 😄
END!!!

Posted on by:

pila profile

Pila louis

@pila

Full-stack software engineer with over 3years of industrial experience. I work well in a fast-paced environment. I am able to rise up to whatever it takes to make an impact in a new environment.

Discussion

pic
Editor guide
 

I've never seen

def __new__(cls):
    return object.__new__(cls)

Usually, they use

return super(class_name, cls).__new__(cls, *args, **kwargs)

I like your version more.