Getting the docstring of a property

A

Andrew Durdin

How do you get the docstring of a property? Suppose I have the following:

class Foo:
def _prop_get(self):
return 1
prop = property(_prop_get, doc="This is the docstring")

foo = Foo()
print foo.prop
print foo.prop.__doc__

This will display "1", followed by the docstring for an integer (which
is the docstring for the int() function). How can I get the property's
docstring as defined in the property() function?
 
J

John Roth

Andrew Durdin said:
How do you get the docstring of a property? Suppose I have the following:

class Foo:
def _prop_get(self):
return 1
prop = property(_prop_get, doc="This is the docstring")

foo = Foo()
print foo.prop
print foo.prop.__doc__

This will display "1", followed by the docstring for an integer (which
is the docstring for the int() function). How can I get the property's
docstring as defined in the property() function?

You tried to access it from the instance. However, accessing it from the
instance invoked the property, returning 1, before it attempted to
access the docstring.

You need to get it from the class. That is:

Foo.prop.__doc__

should do what you want.

John Roth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top