Best way to do attribute docstrings?
Ken Kuhlman
kskuhlman at gmail.com
Tue Sep 25 21:41:31 EDT 2007
Replying to myself in case someone finds this interesting.
Anyway, I took another shot at this with a little fresher mind, and it
was quickly obvious that I was trying to force attributes to behave
more like classes. It was a small step from there to creating a
factory function to return instances of the appropriate class. I'm
much happier with the result than either of the two previously posted
kludges.
# A factory function for generating attributes that can be annotated.
def attr(value, doc=None):
base = type(value)
if base == bool:
# bool class can't be used as a base, so we make one that can.
class MyBool(int):
def __str__(self):
return str(bool(self))
__repr__ = __str__
base = MyBool
class FancyAttr(base):
pass
fa = FancyAttr(value)
fa.__doc__ = doc
return fa
class Animal(object):
four_legs = attr(value = True, doc = "Animal has four legs")
favorite_food = attr('cheese', doc = "Animal's favorite food")
has_fur = attr(False)
has_fur.__doc__ "Animal has fur"
print Animal.four_legs.__doc__, '::', Animal.four_legs
print Animal.favorite_food.__doc__, '::', Animal.favorite_food
print Animal.has_fur.__doc__, '::', Animal.has_fur
More information about the Python-list
mailing list