Python "self" convention __init__ vs method -
class myclass(object): def __init__(self): self.var = "hi" def some_method(self): print self.var #for example below myclass= myclass()
so understand following statements equivelent.
myclass.some_method() myclass.some_method(myclass)
it takes object
, passes first argument self
some_method
.
but when :
myclass= myclass()
how flow work?
i assuming different, , magic happens behind scenes (someone has memory allocate).
how translate __init__(self)
? passed __init__
myclass
?
myclass= myclass()
calls myclass.__new__
method create instance of myclass
. after myclass.__init__
called instance first argument.
see doc object.__new__
:
object.
__new__
(cls[, ...])called create new instance of class cls.
object.
__init__
(self[, ...])called when instance created.
Comments
Post a Comment