function which knows who has called it.

Ovidiu Predescu ovidiu at
Mon Aug 16 20:43:27 EDT 1999

On Mon, 16 Aug 99 22:12:49 GMT, philh at (Phil Hunt) wrote:

> Is if possible in Python to have a function that prints the name
> of the function that called it. (I'm thinking of using this in
> some debugging code I want to write).

Yes, it's possible, take a look at the traceback module. I use this piece of
code to print the name of function that invokes the 'should' function:

import traceback

def should (condition, message=None):
    if (not condition):
        methodName = traceback.extract_stack()[-2][2]
        fail ("%s: %s" % (methodName, message))

Ovidiu Predescu <ovidiu at>  (inside HP's firewall only)

More information about the Python-list mailing list