Hello, I try to use python descriptors to define attributes with default value (the code is reported below). But apparently, it breaks the docstring mechanism.
help(Basis) shows the right help but help(Rectangle) shows only two lines : " Help on class Rectangle in module basis2: Rectangle = <class 'basis2.Rectangle'> " If the Rectangle.length attribute is removed, the help is OK. Secondly, the __doc__ attribute of a PhysicalValue instance doesn't seem to be read. I don't understand. Any idea ? Thanks a lot Cyril. # A descriptor class with default value handling class PhysicalValue(object): """ A physical value descriptor """ def __init__(self, default_value): self.default_value = default_value self.__doc__ = "Hello from Physical Value" def __get__(self, obj, type=None): if obj.__dict__.has_key(self.key): return getattr(obj, self.key) else: return self.default_value def __set__(self, obj, value): if value is DefaultValue: setattr(obj, self.key, self.default_value) else: setattr(obj, self.key, value) # A meta class which adds instance attributes # If hasattr(cls, "attr") then add "_attr" attribute. class MyMetaClass(type): def __init__(cls, name, bases, dct): super(MyMetaClass, cls).__init__(name, bases, dct) print "Add property to ", name def init(self): pvl = [item for item in cls.__dict__.items() if isinstance(item[1], PhysicalValue)] for pv in pvl: print "Add _%s property to %s" % (pv[0], name) cls.__dict__[pv[0]].key = "_" + pv[0] setattr(self, "_" + pv[0], getattr(self, pv[0])) cls.__init__ = init # A basis class class Basis(object): """ Tempest basis class """ __metaclass__ = MyMetaClass # A concrete class class Rectangle(Basis): """ A beautiful Rectangle """ length = PhysicalValue(12.) -- http://mail.python.org/mailman/listinfo/python-list