Connecting Tech Pros Worldwide Forums | Help | Site Map

Getting the name of a variable which was used as a functionparameter.

Ravi Kotecha
Guest
 
Posts: n/a
#1: Jul 21 '08
Hi guys,

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.
Quote:
Quote:
Quote:
>>def a(a):
.... for k,v in sys._getframe(1).f_locals.items():
.... if id(v) == id(a):
.... print k
....
Quote:
Quote:
Quote:
>>hello = 123
Quote:
Quote:
Quote:
>>a(hello)
hello

## pretty cool.


It's totally useless but I wanted to do it for logging purposes. It
will still return weird results if you have two labels pointing to the
same object but I'm not sure how to get around that, any ideas?

Regards,
Ravi Kotecha

Marc 'BlackJack' Rintsch
Guest
 
Posts: n/a
#2: Jul 21 '08

re: Getting the name of a variable which was used as a functionparameter.


On Mon, 21 Jul 2008 09:01:10 -0700, Ravi Kotecha wrote:
Quote:
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.
>
Quote:
Quote:
>>>def a(a):
... for k,v in sys._getframe(1).f_locals.items():
... if id(v) == id(a):
... print k
...
>
Quote:
Quote:
>>>hello = 123
>
Quote:
Quote:
>>>a(hello)
hello
>
## pretty cool.
Or ugly hack. Beauty lies in the eye of the beer holder…
Quote:
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
Ravi Kotecha
Guest
 
Posts: n/a
#3: Jul 21 '08

re: Getting the name of a variable which was used as a functionparameter.


Of course I wouldn't, it is a total hack, mostly useless but fun. I
tried to do it after someone in #python efnet said it was impossible!

On Jul 21, 9:56*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
Quote:
On Mon, 21 Jul 2008 09:01:10 -0700, Ravi Kotecha wrote:
Quote:
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.
>
Quote:
Quote:
>>def a(a):
... * * for k,v in sys._getframe(1).f_locals.items():
... * * * * if id(v) == id(a):
... * * * * * * print k
...
>
Quote:
Quote:
>>hello = 123
>
Quote:
Quote:
>>a(hello)
hello
>
Quote:
## pretty cool.
>
Or ugly hack. *Beauty lies in the eye of the beer holder…
>
Quote:
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
Fredrik Lundh
Guest
 
Posts: n/a
#4: Jul 21 '08

re: Getting the name of a variable which was used as a functionparameter.


Ravi Kotecha wrote:
Quote:
Of course I wouldn't, it is a total hack, mostly useless but fun. I
tried to do it after someone in #python efnet said it was impossible!
your function only finds *some* name (if it finds a name at all), not
*the* name, so you haven't really proven that someone wrong yet.

you can get a lot closer by using more radical approaches, but I don't
think it's possible to do this in a fully robust way, at least not from
the Python level.

</F>

Closed Thread