473,499 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

User Access to the docstring of a property

Is there some way that the user can access the docstring specified for a
property?

Please see the example below:

# propDocTest
class A(object):
def __init__(self, value):
self.value= value
def vGet(self):
return self.value
V= property (fget= vGet, doc="Get Value.")

a= A(22)
print a.vGet()
print a.V
print a.V.__doc__ # this gives the docstring for the value returned
help(a.V) # this gives the docstring for the class/type of
the value returned

Colin W.

Oct 20 '06 #1
4 1324
Colin J. Williams schrieb:
Is there some way that the user can access the docstring specified for a
property?
You need to access it using the class, not an instance of it.

class Foo(object):

@apply
def prop():
def fget(self):
return 10
def fset(self, value):
pass
doc = "this is a docstring"
return property(**locals())

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

print f.__class__.prop.__doc__
Diez
Oct 20 '06 #2

Colin J. Williams wrote:
Is there some way that the user can access the docstring specified for a
property?
Do keep in mind that the docstring is not guaranteed to be available.
If
the application is run with optimization turned on, docstrings are
usually
optimized out. Docstrings are handy for reading code and maybe for
debugging, but should not be relied upon for "users", as opposed to
developers.

-- George Young
Please see the example below:

# propDocTest
class A(object):
def __init__(self, value):
self.value= value
def vGet(self):
return self.value
V= property (fget= vGet, doc="Get Value.")

a= A(22)
print a.vGet()
print a.V
print a.V.__doc__ # this gives the docstring for the value returned
help(a.V) # this gives the docstring for the class/type of
the value returned

Colin W.
Oct 20 '06 #3
George,

Thanks to Dietz and yourself.

Yes, I should have referenced the class, rather than the instance.
However, for methods, the docstring is revealed for an instance.

Colin W.

PS It would help if someone could explain the use of @apply in the
example Dietz gave. The documentation gives no reference to @ or to
decorators.

ge**********@gmail.com wrote:
Colin J. Williams wrote:
>Is there some way that the user can access the docstring specified for a
property?

Do keep in mind that the docstring is not guaranteed to be available.
If
the application is run with optimization turned on, docstrings are
usually
optimized out. Docstrings are handy for reading code and maybe for
debugging, but should not be relied upon for "users", as opposed to
developers.

-- George Young
>Please see the example below:

# propDocTest
class A(object):
def __init__(self, value):
self.value= value
def vGet(self):
return self.value
V= property (fget= vGet, doc="Get Value.")

a= A(22)
print a.vGet()
print a.V
print a.V.__doc__ # this gives the docstring for the value returned
help(a.V) # this gives the docstring for the class/type of
the value returned

Colin W.
Oct 21 '06 #4
Colin J. Williams schrieb:
George,

Thanks to Dietz and yourself.

Yes, I should have referenced the class, rather than the instance.
However, for methods, the docstring is revealed for an instance.

Colin W.

PS It would help if someone could explain the use of @apply in the
example Dietz gave. The documentation gives no reference to @ or to
The decorator semantics are simple:
@a
@b(argument)
def foo():
pass

get translated to

foo = a(b(argument)(foo))

as a decorator is nothing but function that is called with one thing,
and returns something else. or the same thing, by the way.

Now apply was important back then before the *args and **keywordargs
shortcuts where introduced.

It basically takes a function as first argument, and possibly a list
and/or dict, and invokes the function with that argumens in place.

So

def foo(a):
print a

apply(foo, [10])

works as simple as

foo(10)
locals() is a built-in that returns a dictionary which contains all the
locally known names.

And property is a descriptor-creation-function, that has this signature:

property(fget, fset, fdel, doc)

Now we have all we need to decompose that neat property-creation-trick
that doesn't pollute the class' namespace:

class Foo(object):
@apply
def bar():
def fget(self):
return self._bar
doc = "bar property"
return property(**locals())

What happens is this:

the decoration gets translated to this:

bar = apply(bar)

which does simply invoke bar, and assign the result to the name bar in
the class.

invoking bar executes the property function, which is fed with the
dictionary of the locals - coincidently named after the named arguments
property takes.
What I really do love about this: it doesn't pollute the namespace.

Regards,

Diez
Oct 21 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2070
by: Andrew Durdin | last post by:
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...
5
2860
by: c676228 | last post by:
Hi, I guess I am confused. In aspx script, I mean (you won't use Codebehind="enrollinfo.aspx.vb", but mix code with html and code together) You can access user control's property directly. Since I...
6
2041
by: SanPy | last post by:
The subject of this message might be a little cryptic, so here's an example of what I mean: def foo(): """doc string of foo""" print foo.__doc__ doc string of foo What I want to know is...
3
1557
by: cyril giraudon | last post by:
Hello, I try to use python descriptors to define attributes with default value (the code is reported below). But apparently, it breaks the docstring mechanism. help(Basis) shows the right...
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.