access __doc__ from within function without reference to function name

Duncan Booth duncan.booth at invalid.invalid
Tue Oct 2 17:31:32 CEST 2007


Tim Chase <python.list at tim.thechases.com> wrote:

>> So basically, my question is: is there a way to access a function from
>> within itself without using its name?
> 
> 
> Well, I don't know if it's the best way to do it, but the 
> following code I just threw together does the trick for me:

The original request was to do it without using the function's name, but 
you are depending on that name so your code is easy enough to break. e.g. 
change the definition of f1 to:

   def f2(func):
     "this is f1"
     s = func()
     print s
     return s
   f1 = f2
   del f2
   
and the output is:

A function docstring
A method's docstring
A class docstring
A module docstring
If you got here, there's something I missed
> c:\temp\docstring.py(18)get_doc_string()
-> return funcname
(Pdb)



More information about the Python-list mailing list