473,388 Members | 1,209 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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.
>>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.
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
Jul 21 '08 #1
3 929
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
Jul 21 '08 #2
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:
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
Jul 21 '08 #3
Ravi Kotecha wrote:
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>

Jul 21 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Carolyn Speakman | last post by:
Hi, I need to get a directory listing from a client machine. The directory will be specified by the user and the results will be stored on the server in an xml file. I've been trying to use the...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
14
by: Geoff Cox | last post by:
Hello, I am trying to get each "Situation - etc" in document.getElementById("Slider1ValueText").innerHTML += escape("\n") + "Situation - " + title + ": "; to start on a new line in the...
15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
10
by: darrel | last post by:
I have this structure: mypage.aspx (class = mypage) myusercontro.ascx On the mypage.aspx I can declare a variable: animal = "monkey" I can read this from the UC by simply doing this:...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
41
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be...
3
by: MDCdeveloper | last post by:
Hello all, and thank you in advance for any feedback/help you might post. I need help getting a web page element id out of a string variable to be used within my c# code behind page. I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.