__slots__ questions (v2.2.2)

Holden Caulfield phoebe_1 at att.net.remove_me
Tue Oct 15 12:51:11 EDT 2002


Greetings,
1)
It looks the the __slots__ bug that people have reported earlier, where a
'slotted' variable starting with '__' (double
underscore is name mangled is still not fixed in v2.2.2. Are there any plans
to fix this?
e.g
class X(object):
    __slots__ = [ '__x']
    def __init__(self):
        self.__x  = None

The above craps out with the Attribute error '_X__x' not found.

2)
Also, in python 2.2.2 as in 2.2.1, a variable in the '__slots__' *must*
still be initialized 'before use'. I do not whether
it is supposed to work that way. If it is, then it seem very
counter-intuitive (well, if you are in the new features bandwagon). To
demonstrate:
class X(object):
    __slots__ = ['_x']

xobj = X()
print xobj._x

the above gives attribute error. (in v2.2.1 and v2.2.2). I was expecting
that if you use '__slots__', all the 'slotted'
variables would get 'instantiated' when an instance is created and default
to None. But now, I have to explicitly
initialize by providing a __init__ method and setting '_x' to something.
To put it another way, I cannot 'get' a slotted variable *unless*  it has
been "set" first .

Currently, I am using the 'metaclass' technique to alleviate the above
problem. But I would rather have python support
it.

Thanks





More information about the Python-list mailing list