[Tutor] RE: Getting a function name from within itself

Bill Bell bill-bell@bill-bell.hamilton.on.ca
Mon, 11 Jun 2001 12:29:55 -0400


> On Mon, 11 Jun 2001, Andrew Wilkins wrote:

> I am posting a question which is not specific to python. Is there a
> way to get the name of a function from within itself at runtime ?
> Suppose that i have a function f1. At the run time when it
> encounters some condition ( errors or some condition which i want
> to let the user know by printing), I want to say that this warning
> originated from this function f1, without actually hard coding that
> name in the print statement. 

Although your question is not specific to Python solutions are, in 
general, specific to each language and sometimes to each 
language's implementation.

A recipe is available for Python on the Activestate site.

http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52315

For Perl, as another example, see Recipe 10.4 in "Perl Cookbook" 
(Christiansen and Torkington), "Determining Current Function 
Name".

Bill

Bill Bell, Software Developer