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

John Belmonte john at neggie.net
Sun Apr 18 11:35:50 EDT 2004


Fred L. Drake, Jr. wrote:
> """Sample implementation of the docslots idea."""
> 
> import sys
> 
> 
> class slotproperty(object):
>     def __init__(self, name, docstring):
>         self.__name__ = name
>         self.__doc__ = docstring
>         self._slotname = "_slotproperty_" + name
> 
>     def __get__(self, inst, cls=None):
>         return getattr(inst, self._slotname)
> 
>     def __set__(self, inst, value):
>         setattr(inst, self._slotname, value)
> 
> 
> def docslots(**kw):
>     namespace = sys._getframe(1).f_locals
>     __slots__ = namespace.get("__slots__", ())
>     __slots__ = list(__slots__)
>     for name, docstring in kw.iteritems():
>         prop = slotproperty(name, docstring)
>         if name in __slots__:
>             __slots__.remove(name)
>         __slots__.append(prop._slotname)
>         namespace[name] = prop
>     namespace["__slots__"] = tuple(__slots__)

Thanks, that at least gives me a framework to try out some ideas.

I'm concerned about something though.  Doesn't this implementation 
impose an overhead on access of slots with doc strings, or can a C 
implementation be made just as efficient as normal slots?

I'm also wondering about Guido's comment.  Even if the __slots__ handler 
were extended to handle docstrings directly via dict values, wouldn't 
metaclasses still be free to intercept the dict for other uses?

-John


-- 
http:// if  ile.org/



More information about the Python-Dev mailing list