[Python-Dev] slots, properties, descriptors, and pydoc (original) (raw)

John Belmonte john at neggie.net
Thu Apr 22 08:15:22 EDT 2004


Guido van Rossum wrote:

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")}

Wouldn't it be just as future-proof to make slots a list of bare slot names or objects? One advantage is that slot names don't have to be carried around externally to the objects. Moreover, using standard attribute names like name and doc will make the solution more future-proof.

The default metaclass will handle slots along these lines:

 for item in obj.__slots__:
     if isinstance(item, StringTypes):
         slot_name, slot_doc = item, None
     else:
         slot_name = item.__name__
         slot_doc = getattr(item, '__doc__', None)

In that way, the default metaclass does not impose any restrictions on what the slot objects are.

Example usage:

 class MySlot(object):
     def __init__(self, name, doc=None):
         self.__name__ = name
         self.__doc__ = doc

 class C(object):
     __slots__ = ['slot1',
                  MySlot('slot2', 'this is the docstring')]

-John

-- http:// ift ile.org/



More information about the Python-Dev mailing list