Printing __doc__

G

gtb

Greetings,

Don't know the daily limit for dumb questions so I will ask one or
more.

In a function I can use the statement n =
sys._getframe().f_code.co_name to get the name of the current
function. Given that I can get the name how can I print the __doc__
string? I cant use the following, it will tell me to bugger off as the
string has no such attribute.

def spam(self):
n = sys._getframe().f_code.co_name
print n.__doc__ #Wrong
print __doc__ #No good either
#....


thanx,

gtb
 
T

Terry Reedy

| Greetings,
|
| Don't know the daily limit for dumb questions so I will ask one or
| more.
|
| In a function I can use the statement n =
| sys._getframe().f_code.co_name to get the name of the current
| function. Given that I can get the name how can I print the __doc__
| string? I cant use the following, it will tell me to bugger off as the
| string has no such attribute.
|
| def spam(self):
| n = sys._getframe().f_code.co_name
| print n.__doc__ #Wrong
| print __doc__ #No good either
| #....

The docstring you are looking for is attached to the *function* object as
..__doc__ and .func_doc. Frame.f_code is a *code* object. It has a
boilerplate doc string, but not the one you want. As near as I can tell,
frames do not keep references to the func object but only the code object,
which is all it needs to run the code.

I believe tracebacks use co_filename and co_name to find the text of a
function. You could try to parse out the doc string from there.

Terry Jan Reedy
 
P

Peter Otten

gtb said:
In a function I can use the statement n =
sys._getframe().f_code.co_name to get the name of the current
function. Given that I can get the name how can I print the __doc__
string? I cant use the following, it will tell me to bugger off as the
string has no such attribute.

def spam(self):
n = sys._getframe().f_code.co_name
print n.__doc__ #Wrong
print __doc__ #No good either
#....
.... return sys._getframe(1).f_code.co_consts[0]
........ print "My docstring is %r" % docstring()
........ "bar's docstring"
.... print "My docstring is %r" % docstring()
....My docstring is "bar's docstring"

No idea how brittle that might be, though.

Peter
 
G

gtb

| Greetings,
|
| Don't know the daily limit for dumb questions so I will ask one or
| more.
|
| In a function I can use the statement n =
| sys._getframe().f_code.co_name to get the name of the current
| function. Given that I can get the name how can I print the __doc__
| string? I cant use the following, it will tell me to bugger off as the
| string has no such attribute.
|
| def spam(self):
| n = sys._getframe().f_code.co_name
| print n.__doc__ #Wrong
| print __doc__ #No good either
| #....

The docstring you are looking for is attached to the *function* object as
.__doc__ and .func_doc. Frame.f_code is a *code* object. It has a
boilerplate doc string, but not the one you want. As near as I can tell,
frames do not keep references to the func object but only the code object,
which is all it needs to run the code.

I believe tracebacks use co_filename and co_name to find the text of a
function. You could try to parse out the doc string from there.

Terry Jan Reedy

Thanks for posting.

OK, .__doc__ or .func_doc

But still the question remains. I cannot use
print .__doc__
OR
print .func_doc

within the function.
 
G

gtb

gtb said:
In a function I can use the statement n =
sys._getframe().f_code.co_name to get the name of the current
function. Given that I can get the name how can I print the __doc__
string? I cant use the following, it will tell me to bugger off as the
string has no such attribute.
def spam(self):
n = sys._getframe().f_code.co_name
print n.__doc__ #Wrong
print __doc__ #No good either
#....

... return sys._getframe(1).f_code.co_consts[0]
...>>> def foo():

... print "My docstring is %r" % docstring()
...>>> def bar():

... "bar's docstring"
... print "My docstring is %r" % docstring()
...>>> foo()

My docstring is None>>> bar()

My docstring is "bar's docstring"

No idea how brittle that might be, though.

Peter

Thanks.

Pardon my ignorance, but brittle?
 
C

Cameron Laird

.
.
import sys
def docstring():

... return sys._getframe(1).f_code.co_consts[0]
...>>> def foo():

... print "My docstring is %r" % docstring()
...>>> def bar():

... "bar's docstring"
... print "My docstring is %r" % docstring()
...>>> foo()

My docstring is None>>> bar()

My docstring is "bar's docstring"

No idea how brittle that might be, though.

Peter

Thanks.

Pardon my ignorance, but brittle?

Peter is trying to communicate that some aspects of Python are
quite conservative, well-documented, and unlikely to change
across versions or implementations. The syntax of, let's say,
"def", is an example of such an aspect.

Peter has no particular evidence about the guarantees of the
sys._getframe(1).f_code.co_consts[0]
resolution; for all he or I know, the core Python maintainers
might change in an upcoming release the implementation of
co_consts. That's what I believe he intends you understand by
"brittle".
 
T

Terry Reedy

|| Peter is trying to communicate that some aspects of Python are
| quite conservative, well-documented, and unlikely to change
| across versions or implementations. The syntax of, let's say,
| "def", is an example of such an aspect.
|
| Peter has no particular evidence about the guarantees of the
| sys._getframe(1).f_code.co_consts[0]
| resolution; for all he or I know, the core Python maintainers
| might change in an upcoming release the implementation of
| co_consts. That's what I believe he intends you understand by
| "brittle".

There is also no reason to expect that such code would work with Python
interpreters other than CPython.

tjr
 
B

Bart Ogryczak

Greetings,

Don't know the daily limit for dumb questions so I will ask one or
more.

In a function I can use the statement n =
sys._getframe().f_code.co_name to get the name of the current
function. Given that I can get the name how can I print the __doc__
string? I cant use the following, it will tell me to bugger off as the
string has no such attribute.

def spam(self):
n = sys._getframe().f_code.co_name
print n.__doc__ #Wrong
print __doc__ #No good either
#....

print eval(n+'.__doc__')
 

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,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top