Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does __init__ method return in python

Tags:

python

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?

like image 831
tez Avatar asked Aug 16 '12 05:08

tez


People also ask

What does an __ init __ method return?

__init__ doesn't return anything and should always return None .

Should __ init __ return anything?

"__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 .

What does __ init __ do in Python?

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.

Can Init method in Python return value?

__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.


1 Answers

__init__() returns None. It is __new__() that returns the new instance.

like image 69
Ignacio Vazquez-Abrams Avatar answered Sep 21 '22 15:09

Ignacio Vazquez-Abrams