Interview Question
thanks for replying.
can we call the __init_ outside the class?if not then why not?
if we do not provide __init_ definition would there be any default init operator as cpp makes(default constructor)?
could not get 1st query... 'can we call the __init_ outside the class' . if you defined inside class...then only way to call is through class instance object ( automatically ) or by self.__init__()
from python org docs :-
"Class objects are described below. When a class object is called, a new class instance (also described below) is created and returned. This implies a call to the class’s __init__() method if it has one. Any arguments are passed on to the __init__() method. If there is no __init__() method, the class must be called without arguments."
so even if you don't create it it will be called
so that means there is default __init__( ) method.If we do not provide any init() there will be a init() method( as cpp gives default constructor).
Am I right?
what I meant in first query was:-
__init__ is private function.But python does not give any concept of private.
Its just name mangling.So by resolving name mangling we should be able to call init().
But when I tried I was not able to call.
1) return value of __init__ is None
- alp April 09, 20102) On class object creation __init__ is called by default (similar to constructor in CPP). You can put ur code in def __init__(self) method too which will get called on object creation
3) you can also call __init__ method anywhere using
self.__init__()