473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

__doc__ of current function?

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__

.... can I write a general docOfCurrentFunction() function so that I
can rewrite it:

def validatePassword(p):
'do something'
print docOfCurrentFunction()

Thanks,

Trevor
Jul 18 '05 #1
3 2395
>
... can I write a general docOfCurrentFunction() function so that I
can rewrite it:

def validatePassword(p):
'do something'
print docOfCurrentFunction()


def docOfCurrentFunction():
import sys
func_name = sys._getframe(1).f_code.co_name
return eval(func_name + '.__doc__')

But I hope that a more intelligent way exists than using 'eval' :)
Ksenia.

Jul 18 '05 #2
Ksenia Marasanova wrote:

... can I write a general docOfCurrentFunction() function so that I
can rewrite it:

def validatePassword(p):
'do something'
print docOfCurrentFunction()


def docOfCurrentFunction():
import sys
func_name = sys._getframe(1).f_code.co_name
return eval(func_name + '.__doc__')

But I hope that a more intelligent way exists than using 'eval' :)


Maybe
import sys
def caller(): .... f = sys._getframe(1)
.... return f.f_globals[f.f_code.co_name]
.... def demo(): .... "Demo docstring"
.... print caller().__doc__
.... demo() Demo docstring


Or can that fail in some cases?

Peter

Jul 18 '05 #3

"Peter Otten" <__*******@web.de> wrote in message
news:ch*************@news.t-online.com...
Ksenia Marasanova wrote:

... can I write a general docOfCurrentFunction() function so that I
can rewrite it:

def validatePassword(p):
'do something'
print docOfCurrentFunction()


def docOfCurrentFunction():
import sys
func_name = sys._getframe(1).f_code.co_name
return eval(func_name + '.__doc__')

But I hope that a more intelligent way exists than using 'eval' :)


Maybe
import sys
def caller(): ... f = sys._getframe(1)
... return f.f_globals[f.f_code.co_name]
... def demo(): ... "Demo docstring"
... print caller().__doc__
... demo() Demo docstring
Or can that fail in some cases?

Peter

Hi.
Yes, it fails for methods:
import sys
def caller(): .... f = sys._getframe(1)
.... return f.f_globals[f.f_code.co_name]
.... class C: .... def m(self):
.... "mmmmmmmmmm"
.... print caller().__doc__
.... c = C()
c.m() Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 4, in m
File "<interactive input>", line 3, in caller
KeyError: 'm'

Jul 18 '05 #4

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

Similar topics

11
by: Ken Varn | last post by:
I want to be able to determine my current line, file, and function in my C# application. I know that C++ has the __LINE__, __FUNCTION__, and __FILE___ macros for getting this, but I cannot find a...
1
by: David Isaac | last post by:
To access the doc string of a property, I have to use the class not an instance. Why? Thanks, Alan Isaac
5
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__...
7
by: gtb | last post by:
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...
6
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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...

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.