access __doc__ from within function without reference to function name

Duncan Booth duncan.booth at invalid.invalid
Tue Oct 2 10:06:45 EDT 2007


SanPy <jhmsmits at gmail.com> wrote:

> So basically, my question is: is there a way to access a function from
> within itself without using its name?
> 
Not really, no. Python is executing a code block, it has no idea which 
function referenced that code block.

You can get the current code object quite easily (inspect.currentframe
().f_code), but the reference from function to code is one way: a single 
code object could be used by multiple functions.

You can get the source code though, so if you are desperate enough you 
could try parsing the source code to extract the docstring:

   print inspect.getsource(inspect.currentframe())

Use the name of the function, that's the best way.



More information about the Python-list mailing list