property docstrings

D

Darren Dale

I am trying to work with properties, using python 2.4.2. I can't get the
docstrings to work, can someone suggest what I'm doing wrong? I think the
following script should print "This is the doc string.", but instead it
prints:

"float(x) -> floating point number

Convert a string or number to a floating point number, if possible."

Thanks,
Darren



class Example(object):
_myattr = 0.0

@apply
def myattr():
doc = """This is the doc string."""
def fget(self):
return self._myattr
def fset(self, value):
self._myattr = value
def fdel(self):
del self._myattr
return property(**locals())

_foo = 1.0
def foo():
doc = """This is the doc string."""
def fget(self):
"""This is the doc string."""
return self._foo
def fset(self, value):
"""This is the doc string."""
self._foo = value
def fdel(self):
"""This is the doc string."""
del self._foo
return locals()
foo = property(**foo())

_bar = 1.0
doc = """This is the doc string."""
def get_bar(self):
return self._bar
def set_bar(self, value):
self._bar = value
def del_bar(self):
del self._bar
bar = property(get_bar, set_bar, del_bar, doc)

a=Example()
print 'myattr docstring:\n', a.myattr.__doc__
print 'foo docstring:\n', a.foo.__doc__
print 'bar docstring:\n', a.bar.__doc__
 
Z

Ziga Seilnacht

Darren said:
I am trying to work with properties, using python 2.4.2. I can't get the
docstrings to work, can someone suggest what I'm doing wrong? I think the
following script should print "This is the doc string.", but instead it
prints:

"float(x) -> floating point number

Convert a string or number to a floating point number, if possible."

Thanks,
Darren

a=Example()
print 'myattr docstring:\n', a.myattr.__doc__
print 'foo docstring:\n', a.foo.__doc__
print 'bar docstring:\n', a.bar.__doc__

change this part to:

print 'myattr docstring:\n', Example.myattr.__doc__
print 'foo docstring:\n', Example.foo.__doc__
print 'bar docstring:\n', Example.bar.__doc__

What happens is that when property is accessed
from an instance, it returns whatever the fget
function returns, and the __doc__ attribute
is then looked up on that object.

To get to the actual property object (and its
__doc__ attribute) you have to access it from
a class.

Ziga
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top