Getting the name of a variable which was used as a function parameter.

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Mon Jul 21 16:56:32 EDT 2008


On Mon, 21 Jul 2008 09:01:10 -0700, Ravi Kotecha wrote:

> I thought this was pretty cool and since I spent 30 mins or so
> goggling before giving up and figuring out myself I thought I'd share
> it with you.
> 
>>>> def a(a):
> ...     for k,v in sys._getframe(1).f_locals.items():
> ...         if id(v) == id(a):
> ...             print k
> ...
> 
>>>> hello = 123
> 
>>>> a(hello)
> hello
> 
> ## pretty cool.

Or ugly hack.  Beauty lies in the eye of the beer holder…

> It's totally useless but I wanted to do it for logging purposes.

Don't use such things in production code, please.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list