Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101939 > unrolled thread
| Started by | Robert <rxjwg98@gmail.com> |
|---|---|
| First post | 2016-01-20 20:00 -0800 |
| Last post | 2016-01-21 18:15 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
How to use the docstring in this property example Robert <rxjwg98@gmail.com> - 2016-01-20 20:00 -0800
Re: How to use the docstring in this property example Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-01-21 15:50 +1100
Re: How to use the docstring in this property example Peter Otten <__peter__@web.de> - 2016-01-21 18:15 +0100
| From | Robert <rxjwg98@gmail.com> |
|---|---|
| Date | 2016-01-20 20:00 -0800 |
| Subject | How to use the docstring in this property example |
| Message-ID | <1cb14a23-9fe4-44e4-ae25-e894852b245c@googlegroups.com> |
Hi,
I read below code snippet on link:
https://docs.python.org/2/library/functions.html#property
--------------
class C(object):
def __init__(self):
self._x = None
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")
If c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.
If given, doc will be the docstring of the property attribute.
////////////////
I can use these:
c.x
c.x=42
del c.x
but I don't know how to get the doctring from the property attribute.
Could you show me how to do that?
Thanks,
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2016-01-21 15:50 +1100 |
| Message-ID | <56a06384$0$1534$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #101939 |
On Thursday 21 January 2016 15:00, Robert wrote:
> Hi,
>
> I read below code snippet on link:
> https://docs.python.org/2/library/functions.html#property
Property docstrings are hard to get to. But with the class C you gave, this
works in the interactive interpreter:
help(C.__dict__['x'])
displays:
I'm the 'x' property.
C.__dict__['x'] will return the property object itself, without running the
getter or setter, so you can access the dostring:
py> C.__dict__['x']
<property object at 0xb7171d9c>
py> C.__dict__['x'].__doc__
"I'm the 'x' property."
But what happens if you inherit the property from some other class?
py> class D(C): pass
...
py> class E(D): pass
...
py> E.__dict__['x']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'x'
In Python 3, you can use the inspect module:
py> obj = inspect.getattr_static(E, 'x')
py> inspect.getdoc(obj)
"I'm the 'x' property."
but in Python 2, you may need to walk the class's MRO by hand. Something
like this:
def getattr_static(obj, name):
if isinstance(obj, type):
for cls in obj.__mro__:
if name in cls.__dict__:
return cls.__dict__[name]
--
Steve
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-01-21 18:15 +0100 |
| Message-ID | <mailman.154.1453396521.15297.python-list@python.org> |
| In reply to | #101939 |
Robert wrote: > Hi, > > I read below code snippet on link: > https://docs.python.org/2/library/functions.html#property > > -------------- > class C(object): > def __init__(self): > self._x = None > > def getx(self): > return self._x > > def setx(self, value): > self._x = value > > def delx(self): > del self._x > > x = property(getx, setx, delx, "I'm the 'x' property.") > If c is an instance of C, c.x will invoke the getter, c.x = value will > invoke the setter and del c.x the deleter. > > If given, doc will be the docstring of the property attribute. > //////////////// > > I can use these: > c.x > c.x=42 > del c.x > > but I don't know how to get the doctring from the property attribute. > Could you show me how to do that? >>> c = C() >>> type(c).x.__doc__ "I'm the 'x' property." But usually you'll see it as part of the help on c: >>> help(c) Help on C in module __main__ object: class C(__builtin__.object) [...] | Data descriptors defined here: [...] | x | I'm the 'x' property.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web