Dive into Python -
It would be tempting but incorrect to
call this the constructor of the
class. It's tempting, because it looks
like a constructor (by convention,
__init__
is the first method defined for the class), acts like one (it's
the first piece of code executed in a
newly created instance of the class),
and even sounds like one (“init”
certainly suggests a constructor-ish
nature). Incorrect, because the object
has already been constructed by the
time __init__
is called, and you
already have a valid reference to the
new instance of the class.
Quote suggests it is incorrect to call __init__
as a constructor because the object is already constructed by the time __init__
is called. But! I have always been under the impression that the constructor is called only after the object is constructed because it is essentially used to initialized the data members of the instance which wouldn't make sense if the object didn't exist by the time constructor was called? (coming from C++/Java background)
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…