__init__ method for containers

Raymond Hettinger python at rcn.com
Wed Dec 12 12:51:42 EST 2007


On Dec 12, 8:41 am, Calvin Spealman <ironfro... at socialserve.com>
wrote:
> It documents that deque.__init__ initializes it, as all __init__
> methods do. All init methods are also assumed to _only_ be called at
> the start of the life of the object and never more than once, so
> breaking that breaks assumption and thus the documentation isn't
> wrong because you are trying to apply it to a state that shouldn't
> exist.

That's overstating the case somewhat.  The init methods are
*typically* called only once but they are not *assumed* to be called
only once. They are in-fact just like any other method except that
their first invocation is automatic.  The clear and re-initialize
behavior of __init__ for lists is evidence.  If list.__init__ was
assumed to be called only once, there would be no need for the step
that clears-out previous values.

Also, it is not obvious what the "right" behavior is.  While
list.__init__ clears previous values, dict.__init__ does not.

In the case of deque.__init__, the lack of clearing behavior is a bug
because the API aspires to mimic lists as much as possible.


Raymond



More information about the Python-list mailing list