Python Forum

Full Version: init vs_init_ while defining method/function?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
while defining method,what is the difference in
this:
def init(...........):

      ................................
 
and 
this:
def _init_(............):
     ................................
In that case, it's just a different name. But __init__ (note the double underscores on both sides, or "dunder") is called automatically when you create an object.
Output:
>>> class Manual: def init(self): self.x = None >>> m = Manual() >>> print m.x Traceback (most recent call last):  File "<pyshell#3>", line 1, in <module>    print m.x AttributeError: Manual instance has no attribute 'x' >>> m.init() >>> print m.x None >>> class Automatic: def __init__(self): self.x = None >>> a = Automatic() >>> print a.x None