__slots__ variables and initialization

Mike Rovner mike at bindkey.com
Wed Jul 31 16:27:47 EDT 2002


"Holden Caulfield" <phoebe_1 at att.net> wrote in message
news:c2595393.0207311037.5bacf413 at posting.google.com...
> Greetings,
>   I don't recall exactly where I read it, but I had read that ALL the
> variables
> in  __slots__ were initialized to None and are automatically part of
> the instance attributes whether they are explicitly created in the
> __init__ method or not.
>  In 2.2.1 this is not what is happening. e.g:
> class X(object):
>   __slots__ = ['_x']
>   def get_x(self):
>       return self._x
>
> >>> x = X()
> >>> x.get_x()
>  I get an attribute error. Obviously, defining and '__init__' method
> and explicitly creating it works. Is this the way it is supposed to
> be?

You can look at  GvR "Unifying types and classes in Python 2.2 (Python
Version: 2.2.1)" (http://www.python.org/2.2.1/descrintro.html):

<quote>
Some noteworthy tidbits and warnings about __slots__:


  a.. An undefined slot variable will raise AttributeError as expected.
(Note that in Python 2.2b2 and earlier, slot variables had the value None by
default, and "deleting" them restores this default value.)
</quote>

So don't rely on it, it was a bug.

WBR,
Mike









More information about the Python-list mailing list