473,624 Members | 2,290 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 1332
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(**loca ls())

f = Foo()
print f.prop
print Foo.prop.__doc_ _

print f.__class__.pro p.__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**********@gm ail.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)(f oo))

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(**loca ls())

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
2079
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 foo.prop print foo.prop.__doc__
5
2870
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 am useing visual studio .net, the html and code are seperated. I have the following aspx code which has two user controls: <%@ Register TagPrefix="Subway" TagName="Peopleinfo" Src="Peopleinfo.ascx" %> <%@ Register TagPrefix="Subway"...
6
2045
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 whether it is possible to call __doc__ against
3
1570
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 help but help(Rectangle) shows only two lines : " Help on class Rectangle in module basis2:
0
8240
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4082
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1487
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.