I have a class defined like so:
class Client():
def __new__(cls):
print "NEW"
return cls
def __init__(self):
print "INIT"
When I use it, I get the following output:
cl = Client()
# INIT
__new__
is not being called. Why?
In the base class object , the __new__ method is defined as a static method which requires to pass a parameter cls . cls represents the class that is needed to be instantiated, and the compiler automatically provides this parameter at the time of instantiation.
self : self represents the instance of the class. By using the "self" keyword all the attributes and methods of the python class can be accessed. __init__ : "__init__" is a reserved method in python classes. It is known as a constructor in object oriented concepts.
Having read your answer, I improve it with
class Client(object):
def __new__(cls):
print "NEW"
return super(Client, cls).__new__(cls)
def __init__(self):
print "INIT"
so that c = Client()
outputs
NEW
INIT
as intended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With