function which knows who has called it.

Ovidiu Predescu ovidiu at cup.hp.com
Tue Aug 17 02:43:27 CEST 1999


On Mon, 16 Aug 99 22:12:49 GMT, philh at vision25.demon.co.uk (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))


Greetings,
-- 
Ovidiu Predescu <ovidiu at cup.hp.com>
http://andromeda.cup.hp.com/  (inside HP's firewall only)
http://www.geocities.com/SiliconValley/Monitor/7464/






More information about the Python-list mailing list