__init__ explanation please

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Jan 14 06:18:44 EST 2008


"A.T.Hofkamp" <hat at se-162.se.wtb.tue.nl> writes:

> while you think you are doing "Person('me', 'here', 31)", you are in
> reality executing "Person.__init__(self, 'me', 'here', 31)", where
> 'self' is refers to a shiny new, empty object created for you.

This is misleading, and founders on many discrepancies, not least of
which is that '__init__' always returns None, yet the 'Person()' call
returns the new instance. So it's quite untrue to say that one is "in
reality" calling the '__init__' method.

What one is "in reality" calling is the '__new__' method of the Person
class. That function, in turn, is creating a new Person instance, and
calling the '__init__' method of the newly-created instance. Finally,
the '__new__' method returns that instance back to the caller.

-- 
 \       "Probably the toughest time in anyone's life is when you have |
  `\         to murder a loved one because they're the devil."  -- Emo |
_o__)                                                          Philips |
Ben Finney



More information about the Python-list mailing list