DEV Community

Discussion on: Python: __init__() is not the only constructor

Collapse
 
veselink1 profile image
Veselin Karaganev

self is normally used when referring to an instance of a class, not the class itself. Also, in your example usage for __new__ you have used cls instead of self, which is not present. You might want to correct that.

Collapse
 
gabrielamram profile image
Gabriel Amram • Edited

__new__ should use cls and not self as it is a "static" method that belongs to the class, not the instance.
See docs.python.org/3/reference/datamo...

Collapse
 
timconroy profile image
Tim Conroy

Absolutely correct @gabriel
per the Python documentation:
" The new(cls[,...]) method is called first to create a new instance of class, cls. It is a static
method that takes the class of which an instance was requested as its first argument.
The return value of new( ) should be the new object instance(usually an instance of class(cls)). "
Semantics of interpretation of the programming language: It is a class 'static' method. :)>

Thread Thread
 
delta456 profile image
Swastik Baranwal

Yes, its right and I had changed it a long time back!

Collapse
 
themysi profile image
Lea Maya

It's actually a classmethod, not a static method but cls is still the recommended name