[Tutor] how to reference a function itself when accessing its private functions?

Emile van Sebille emile at fenx.com
Tue May 5 01:08:38 CEST 2009


On 5/4/2009 3:37 PM Tim Michelsen said...
> Dear Tutors and fellow pythonistas,
> I would like to get access to the private methods of my function.
> 
> For instance:
> Who can I reference the docstring of a function within the function itself?
<snip>
> 
> def show2(str):
>     """prints str"""
>     print str
>     d = self.__doc__
>     print d

 >>> def show2(str):
...     """prints str"""
...     print str
...     print globals()['show2'].__doc__
...
 >>> show2('hello')
hello
prints str
 >>>


This is the easy way -- ie, you know where to look and what name to use. 
  You can discover the name using the inspect module, but it can get 
ugly.  If you're interested start with...

from inspect import getframeinfo, currentframe

HTH,

Emile



More information about the Tutor mailing list