Accessing docstrings at runtime?
Chris Mellon
arkanes at gmail.com
Wed Aug 29 14:28:54 EDT 2007
On 8/29/07, Kenneth Love <klove at tax.ok.gov> wrote:
> How do I print the docstring for a class property?
>
> When I run the code below, I get the docstring for the string module
> and not the one I set for the property.
>
> ---------------------------------------------
> # NOTE: Found in Python docs defining built-in functions (such as
> # property()). FIXED: Bug in getx, setx, and delx where "__x"
> # was misreferenced as "_x".
> class C(object):
> def __init__(self):
> self.__x = None
> def getx(self):
> return self.__x
> def setx(self, value):
> self.__x = value
> def delx(self):
> del self.__x
> x = property(getx, setx, delx, "I'm the 'x' property.")
>
> if __name__ == "__main__"
> y = C()
> y.x = 'test'
> print y.x
> print y.x.__doc__
> ---------------------------------------------
>
> I get the following output:
>
> ---------------------------------------------
> test
> str(object) -> string
>
> Return a nice string representation of the object.
> If the argument is a string, the return value is the same object.
> ---------------------------------------------
>
> What am I doing wrong?
>
You're looking at an instance, not at the class. y.x is going through
the descriptor lookup and is returning the string, so the __doc__ is
the the __doc__ of the string object, as you see.
If you want to look at the property directly, look it up in the class object:
C.x.__doc__
More information about the Python-list
mailing list