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
__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.
__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.
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.
class Point: def __init__(self, data): self.num = data def print_num(self): print(self.num) obj = Point(100) obj.print_num()
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.
class Person: def __new__(cls): return object.__new__(cls) def __init__(self): self.instance_method() def instance_method(self): print('success!') personObj = Person()
__init__ receives the argument self, while
__new__ receives the class
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. 😄