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 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.
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
"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'
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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: 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...
|
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: 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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |