newbie question - identifying name of method

Fredrik Lundh fredrik at pythonware.com
Mon Feb 14 11:02:20 EST 2005


mirandacascade at yahoo.com wrote:

> Does Python provide some sort of mechanism for answering the question:
> what method am I in?
>
> Example: assume the file example1.py contains the following code:
>
> def driver():
>    print 'hello world'
>    print __name__
>    print 'the name of this method is %s' % str(???)
>
> The output I'd like to see is:
>
> hello world
> example1
> driver
>
> and I'd like to be able to see it without hardcoding the string
> 'driver' in the third print statement.  Is there anything I can
> substitute for the ??? that answers the question: what method am I in?

replace str(???) with

    (sys._getframe().f_code.co_name or "???")

</F> 






More information about the Python-list mailing list