User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 427,097 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,330 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 309 | Replies: 7
Reply
Join Date: Jul 2008
Posts: 7
Reputation: db_ozbecool is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
db_ozbecool db_ozbecool is offline Offline
Newbie Poster

getting a variable name

  #1  
Jul 15th, 2008
How to get the NAME of the passed parameter ?
Eg. in the example below how to get printed , inside my_func() , value = "test_var"

def my_func(aaa):
print aaa # <---- this will print the VALUE of passed parameter 'aaa'
# ..... but how to print the NAME of the 'aaa' above as called by a parent function main() - i.e.


how to make it print 'test_var'
def main():
test_var = 123
my_func(test_var)

Note that I do not want to get 'aaa' printed but 'test_var'
Last edited by Tekmaven : Jul 15th, 2008 at 1:16 am. Reason: Code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Posts: 1,451
Reputation: sneekula is on a distinguished road 
Rep Power: 4
Solved Threads: 44
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Virtuoso

Re: getting a variable name

  #2  
Jul 15th, 2008
Local variables are kept in the function's local dictionary which you can access with vars(). To access one function's local dictionary in another function, you have to pass it along:
  1. def my_func(aaa, main_var):
  2. mvar = [key for key, val in main_var.items() if val==aaa][0]
  3. print aaa, main_var, mvar # 123 {'test_var': 123} test_var
  4.  
  5. def main():
  6. test_var = 123
  7. my_func(test_var, vars())
  8.  
  9. main()
No one died when Clinton lied.
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: db_ozbecool is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
db_ozbecool db_ozbecool is offline Offline
Newbie Poster

Re: getting a variable name

  #3  
Jul 15th, 2008
thanks for a reply but can you re-write the mvar = ... line in more 'beginners' friendly format ? :
mvar = [key for key, val in main_var.items() if val==aaa][0]

i.e. few separate lines so that I would understand what ot does ?
Thanks
Last edited by db_ozbecool : Jul 15th, 2008 at 8:41 pm.
Reply With Quote  
Join Date: Oct 2006
Posts: 1,451
Reputation: sneekula is on a distinguished road 
Rep Power: 4
Solved Threads: 44
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Virtuoso

Re: getting a variable name

  #4  
Jul 15th, 2008
I hope you understand this better:
  1. def main():
  2. a = 1
  3. b = 2
  4. c = 3
  5. # local variable dictionary
  6. main_var = vars()
  7. print main_var # {'a': 1, 'c': 3, 'b': 2}
  8.  
  9. # get the key (variable name) of a certain value, for instance 3
  10. mvar = [key for key, val in main_var.items() if val==3][0]
  11. print mvar # c
  12.  
  13. # in somewhat longer form ...
  14. mvar_list = []
  15. # extract the key:value pairs from the dictionary
  16. for key, val in main_var.items():
  17. # if value is 3 save the key in a list
  18. if val == 3:
  19. mvar_list.append(key)
  20. print mvar_list # ['c']
  21. # pick the first item on the list
  22. print mvar_list[0] # c
  23.  
  24. main()
No one died when Clinton lied.
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: db_ozbecool is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
db_ozbecool db_ozbecool is offline Offline
Newbie Poster

Re: getting a variable name

  #5  
Jul 16th, 2008
Great thanks for the time you spent on it. Most appreciated. BTW: where can I find explanation of what this is (from your code above):

... = [key .... ]

i.e. [ followed by a reserved word
Reply With Quote  
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,476
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 10
Solved Threads: 176
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
Kickbutt Moderator

Re: getting a variable name

  #6  
Jul 16th, 2008
the code within [] is called list comprehension and it creates a list
...

q = [process(x) for x in somelist if condition(x)]

is a shorthand notation for ...
q = []
for x in somelist:
    if condition(x):
        q.append(process(x))

...
Last edited by vegaseat : Jul 16th, 2008 at 10:03 am.
May 'the Google' be with you!
Reply With Quote  
Join Date: Jul 2008
Posts: 7
Reputation: db_ozbecool is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
db_ozbecool db_ozbecool is offline Offline
Newbie Poster

getting a variable name - catching a literal data passed

  #7  
Jul 17th, 2008
One more thing :
How to catch (inside my_func()) a situation of receiving a literal value instead of a variable name:
my_func(test_var, vars()) --- > my_func(123, vars())

thus avoiding this error when making a call to main():
< IndexError: list index out of range >

Note: I realize that my_func() is not able to recover the 'key' for literal data passed, but as long as it does not cause a run time error then I would be fine)
Reply With Quote  
Join Date: Jan 2008
Posts: 603
Reputation: ZZucker is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 21
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Practically a Master Poster

Re: getting a variable name

  #8  
Jul 17th, 2008
Nice observation! Trap the error with try/except like this:
  1. def my_func(aaa, main_var=None):
  2. try:
  3. mvar = [key for key, val in main_var.items() if val==aaa][0]
  4. print aaa, main_var, mvar
  5. except:
  6. print aaa
  7. # other code follows
  8. pass
  9.  
  10. def main():
  11. test_var = 123
  12. my_func(test_var, vars())
  13. my_func(123, vars())
  14. my_func(456, vars())
  15. my_func(123)
  16.  
  17. main()
  18.  
  19. """
  20. my output -->
  21. 123 {'test_var': 123} test_var
  22. 123 {'test_var': 123} test_var
  23. 456
  24. 123
  25. """
Last edited by ZZucker : Jul 17th, 2008 at 8:37 am.
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 5:27 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC