Inheritable Slots Metaclass

Rebel Lion r3831110n at gmail.com
Wed May 26 23:33:41 EDT 2010


> I'd be ok with a metatype in the standard library that makes slots
> more transparent, but since slots are intended as an optimization, it
> doesn't really need (and might be detrimental to have) transparency
> for ordinary objects.
>

But why there is __slots__ if it's not indeed needed. we should make
it more usable instead of a hack, which it now looks like.

> However, Aahz will be by shortly to tell you never to use slots.

Obviously he didn't work on graphs, at least huge graphs.

> It seems like a good approach, but the use of decorators to define
> slots is hideous.  I'd recommend creating slots with simple assignment
> instead:
>
> slot = object()
>
> class Foo():
>     __metaclass__ = SlotsMetaclass
>     foo = slot
>
> class Bar():
>     bar = slot
>
> Then replace
>
> "isinstance(v,slot)" with "v is slot"
>
> in the metaclass.
>

This is a style not a problem. My way intents to keep the consistency
with @property

> You don't need to create a temporary class here; you should loop
> through the base classes and inspect their slots.  

As you can see, I'm very lazy. If we got some decision here, we can
surely make a better implementation that this hack.

> Also, you don't
> need to recreate slots that exist in base classes, so you could just
> get all the new slots from the class dict.
>
> You should consider corner cases if you think it should be standard.
> For instance, what if a derived class defined the same slot as a a
> base class--it'll create a new slot in yours but it shouldn't.  What
> if you want to create a subclass that does have a dict?  Can't do it
> in your version.  You need to consider stuff like that.
>

The most arguable point here. I raise this post inorder to make slot
inheritable.
In most cases, if a class uses slots, it is designed to be heavily
used.
Ans subclass of it should be heavily used too, but in this case you
want to create __slots__,
you have to manually lookup __slots__ of bases.

But your corner is considerable. We can offer a __no_slots__ to
disable slots.

In conclusion, my point is:

If you don't want to use slots in common days, you can subclass a
class which doesn't use slots(in most cases)
If you want to use slots, you need inherit parent classes' slots(in a
few cases)
If you don't want to use slots when you are subclassing a class which
uses slots(in few cases):
  use a __noslots__ magic



Regards,




More information about the Python-list mailing list