[Python-Dev] slots, properties, descriptors, and pydoc

Guido van Rossum guido at python.org
Mon Apr 19 11:10:59 EDT 2004


> Guido van Rossum wrote:
> > Anything can be done using metaclasses.  __slots__ is not special
> > once the class exists -- it is a set of instructions for the default
> > metaclass to create a specific set of descriptors (and associated
> > storage).  Another metaclass could use a different convention
> > (although it may have to set __slots__ to let the base metaclass
> > create the associated storage slots).
> 
> My original proposal was to use __slots__ dict values for docstrings in 
> the default metaclass.  You said you'd rather not do that because 
> different metaclasses may want to use the dict value for different 
> purposes.  But from what you've explained, metaclasses are free to 
> interpret the value of __slots__ in any way they choose.  Metaclasses 
> built on top of the default metaclass could translate their __slots__ 
> value to the __slots__ I proposed.

Yes, but *if* the default metaclass assumed a dict contained only
docstrings, this would be the standard usage, and it would be
confusing (and sometimes incompatible) if a custom metaclass had a
different interpretation.  As long as the default metaclass doesn't
have any particular interpretation for the values in the dict, custom
metaclasses can do what they like.

> Are optional tuples any better?  This wouldn't preclude use of dict 
> values for something else.
> 
> class Foo(object):
>      __slots__ = [
>          'slot1',
>          ('slot2', 'description'),
>          ('slot3', """description
>              ...continued"""),
>      ]

But that currently doesn't work.  Tbe most future-proof solution would
be to put some kind of object in the dict values whose attributes can
guide various metaclasses.  Perhaps:

class slotprop(object):
    def __init__(self, **kwds):
        self.__dict__.update(kwds)

class C(object):
    __slots__ = {'slot1': slotprop(doc="this is the docstring"),
                 'slit2': slotprop('doc="another one")}

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list