class Num:
def __init__(self,num):
self.n = num
I read that the __init__
method returns None
.When I perform a=Num(5)
, Num(5)
will call __init__
method of the class.But if __init__
returns None
then a
should reference nothing.But instead a
is referencing the object of Num
Class.How does it happen?So does __init__
return None
or the object of the class?
__init__ doesn't return anything and should always return None .
"__init__" should not return a value. By contract, every Python function returns something, even if it's the None value, which can be returned implicitly by omitting the return statement, or explicitly. The __init__ method is required to return None .
The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.
__init__ method returns a value The __init__ method of a class is used to initialize new objects, not create them. As such, it should not return any value. Returning None is correct in the sense that no runtime error will occur, but it suggests that the returned value is meaningful, which it is not.
__init__()
returns None
. It is __new__()
that returns the new instance.
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