•
•
•
•
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
![]() |
•
•
Join Date: Jul 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
How to get the NAME of the passed parameter ?
Eg. in the example below how to get printed , inside my_func() , value = "test_var"
Note that I do not want to get 'aaa' printed but 'test_var'
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 = 123my_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
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:
python Syntax (Toggle Plain Text)
def my_func(aaa, main_var): mvar = [key for key, val in main_var.items() if val==aaa][0] print aaa, main_var, mvar # 123 {'test_var': 123} test_var def main(): test_var = 123 my_func(test_var, vars()) main()
No one died when Clinton lied.
I hope you understand this better:
python Syntax (Toggle Plain Text)
def main(): a = 1 b = 2 c = 3 # local variable dictionary main_var = vars() print main_var # {'a': 1, 'c': 3, 'b': 2} # get the key (variable name) of a certain value, for instance 3 mvar = [key for key, val in main_var.items() if val==3][0] print mvar # c # in somewhat longer form ... mvar_list = [] # extract the key:value pairs from the dictionary for key, val in main_var.items(): # if value is 3 save the key in a list if val == 3: mvar_list.append(key) print mvar_list # ['c'] # pick the first item on the list print mvar_list[0] # c main()
No one died when Clinton lied.
•
•
Join Date: Oct 2004
Location: Mojave Desert
Posts: 2,476
Reputation:
Rep Power: 10
Solved Threads: 176
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!
•
•
Join Date: Jul 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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)
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)
Nice observation! Trap the error with try/except like this:
python Syntax (Toggle Plain Text)
def my_func(aaa, main_var=None): try: mvar = [key for key, val in main_var.items() if val==aaa][0] print aaa, main_var, mvar except: print aaa # other code follows pass def main(): test_var = 123 my_func(test_var, vars()) my_func(123, vars()) my_func(456, vars()) my_func(123) main() """ my output --> 123 {'test_var': 123} test_var 123 {'test_var': 123} test_var 456 123 """
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Session variable (ASP.NET)
- session variable:again (ASP.NET)
- Undefined variable: PHP_SELF (PHP)
- Need Help with variable initialization (Java)
- variable function parameters functionname(int a, ...) (C)
- Retreiving a Variable from a URL (PHP)
- Can draw one Frame in a variable? (Java)
- Pipe inside variable isn't working in bash (Shell Scripting)
Other Threads in the Python Forum
- Previous Thread: wxPython: can you sort a TreeListCtrl?
- Next Thread: Twill - using 'find' command from python code



Linear Mode