Doc strings in descriptors

K

Kevin D. Smith

I have a simple descriptor to create a cached property as shown below.

class cproperty(object):
""" Property whose value is only calculated once and cached """
def __init__(self, func):
self._func = func
self.__doc__ = func.__doc__
def __get__(self, obj, type=None):
if obj is None:
return self
try:
return getattr(obj, '@%s' % self._func.func_name)
except AttributeError:
result = self._func(obj)
setattr(obj, '@%s' % self._func.func_name, result)
return result

The problem is that when I use the help() function on them, I don't get
the doc string from the function that is being wrapped. Instead, I get
the following:

hasEmployees = <StudioManager.utils.cproperty object at 0xf126f0>

What do I need to do to get the doc string of the wrapped function to
apper when using help()?
 
S

Steven D'Aprano

I have a simple descriptor to create a cached property as shown below. ....
What do I need to do to get the doc string of the wrapped function to
apper when using help()?

Call it on the class, not the instance:

.... def getx(self): return "spam"
.... def setx(self, value): return None # and do nothing
.... x = property(getx, setx, None, "This is my docstring")
....'str(object) -> string\n\nReturn a nice string representation of the
object.\nIf the argument is a string, the return value is the same
object.''This is my docstring'
 
Ð

Дамјан ГеоргиевÑки

I have a simple descriptor to create a cached property as shown below.
....
The problem is that when I use the help() function on them, I don't
get the doc string from the function that is being wrapped. ....
What do I need to do to get the doc string of the wrapped function to
apper when using help()?

take a look at
http://pypi.python.org/pypi/decorator
and possibly
http://docs.python.org/library/functools.html#functools.update_wrapper


--
дамјан ( http://softver.org.mk/damjan/ )

Real men don't use backups, they post their stuff on a public ftp server
and let the rest of the world make copies.
-- Linus Torvalds
 
G

Gabriel Genellina


That doesn't help. The descriptor *does* have its docstring copied from
the function. But pydoc (the module behind the help system) doesn't handle
the case - "normal" properties are handled specially, but a generic
descriptor like that is not understood.

(and it's not so easy to fix - pydoc does a lot of things, all intermixed:
it tries to discover all
methods and attributes, determine which are relevant, extract
documentation from them, organize and clasify such documentation, generate
HTML files/text files/present it to the user/run a webserver/invoke the
pager... Too much for my taste, but I disgress...)
 

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
474,260
Messages
2,571,039
Members
48,768
Latest member
first4landlord

Latest Threads

Top