473,513 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing __doc__

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

Mar 21 '07 #1
7 3558

"gtb" <go*************@hotmail.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
| 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

Mar 21 '07 #2
gtb wrote:
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
#....
>>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
Mar 21 '07 #3
gtb
On Mar 21, 3:11 pm, "Terry Reedy" <tjre...@udel.eduwrote:
"gtb" <goodTweetieB...@hotmail.comwrote in message

news:11**********************@d57g2000hsg.googlegr oups.com...
| 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.
Mar 21 '07 #4
gtb
On Mar 21, 3:35 pm, Peter Otten <__pete...@web.dewrote:
gtb wrote:
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
#....
>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?

Mar 21 '07 #5
In article <11**********************@e1g2000hsg.googlegroups. com>,
gtb <go*************@hotmail.comwrote:
>On Mar 21, 3:35 pm, Peter Otten <__pete...@web.dewrote:
Mar 21 '07 #6

"Cameron Laird" <cl****@lairds.uswrote in message
news:f5************@lairds.us...
|| 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

Mar 22 '07 #7
On Mar 21, 8:47 pm, "gtb" <goodTweetieB...@hotmail.comwrote:
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__')


Mar 22 '07 #8

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

Similar topics

3
2401
by: Trevor Taylor | last post by:
Hi, I thought it would be simple but couldn't figure it out, nor find it addressed already: I can do: def validatePassword(p): 'do something' print validatePassword.__doc__
3
1812
by: Ron_Adam | last post by:
Hi, Sometimes it just helps to see what's going on, so I've been trying to write a tool to examine what names are pointing to what objects in the current scope. This still has some glitches,...
6
3046
by: Kamilche | last post by:
I have a large project that is getting complex, and I would like to print the docstrings without importing the modules. The only Python utility I could find references is apparently defunct and...
2
1995
by: Sheldon | last post by:
Good day, I would like to know if there is a way to print all the variables set in a python program with having to write "print variable" on all? sincerely, Sheldon
9
1410
by: s99999999s2003 | last post by:
hi say i have variables like these var1 = "blah" var2 = "blahblah" var3 = "blahblahblah" var4 = "...." var5 = "...".. bcos all the variable names start with "var", is there a way to
5
1309
by: Gabriel Genellina | last post by:
Hello I have a script starting with a docstring. After compiling it with compile(), is there any way I could get the docstring? __doc__ on the code object doesn't work. I can't use __import__...
6
1374
by: James T. Dennis | last post by:
Tonight I discovered something odd in the __doc__ for tempfile as shipped with Python 2.4.4 and 2.5: it says: This module also provides some data items to the user: TMP_MAX - maximum number...
10
2094
by: HYRY | last post by:
I have the following questions, I am using Python 2.4.2 19167152 #1 11306608 #1 1. the address of a.append and list.append is different, can I get the address of...
6
2043
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...
0
7267
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
7391
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,...
0
7553
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
7542
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
5697
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
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
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
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.