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 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
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
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.
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?
In article <11**********************@e1g2000hsg.googlegroups. com>,
gtb <go*************@hotmail.comwrote:
>On Mar 21, 3:35 pm, Peter Otten <__pete...@web.dewrote:
"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
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__') This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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__
|
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,...
|
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...
|
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
|
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
| |
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__...
|
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...
|
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...
|
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...
|
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,...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |