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

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).flocals 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