__slot__ issues

Eric Texier erict at millfilm.co.uk
Fri Jul 5 04:05:23 EDT 2002


Arthur Siegel wrote:

> (The following is copy of my post to tutor at python.org.
> Since I believe it to involve a (minor) bug issue, I
> thought I'd copy it here for possible comment)
>

I don't thing it is a bug and I like it like this. I use it, not only to save
memory but also to force myself to declare and init all the variables
instance. When I am there,  editing the __slots__  I can use the time
to actually set a default value in the __init__. (nasty c++ habit)

>
> Apologies to the folks who do not share my new found interest in slots.
>
> Which I am starting to think of as the quark of the Python object world.
>
> >>> class A(object):
>  __slots__=("a")
> >>> class B(A):
>  __slots__=("b")
> >>> b=B()
> >>> b.__slots__
> 'b'
> >>> b.a=4
> In other words "a" is somehow acting as a slot for
> B instances, but is not *in* B.__slots__.
> How would one then reference, introspect, whatever
> as to what are in fact available slots for instances of B?
>

It will not be hard to have a method to build up a the concatenate
slots from parent class:    self.__class__.__bases__



>
> And a possibly minor bug:
>
> >>> class A(object):
>  __slots__="a"
>
> >>> class B(object):
>  pass
>
> >>> class C(A,B):
>  __slots__=("b")
>
> >>> c=C()
> >>> c.a=1
> >>> c.b=2
> >>> c.c=3
> Traceback (most recent call last):
>   File "<pyshell#12>", line 1, in ?
>     c.c=3
> SystemError: C:\Code\221\Objects\dictobject.c:511: bad argument to internal
> function
>
> If B is a classic class and I do the same multi-inheritance I get the
> expected error message:
>
> AttributeError: 'C' object has no attribute 'c'
>
> So its the error message, not the behavior that I am pointing to as probably
> unintended.
>
> Art
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor




More information about the Python-list mailing list