Is it possible to get the name of the passed parameter in Python?
Specifically, I want to be able to do this:
def a(name):
print "the actual passed parameter is %s" %(???)
What it solves?
So if I need to distribute coins in bags and baskets, say bag_list and basket_list, I need not send an explicit identifier saying 'bag' or 'basket'
Which is how I've currently worked around it. I'm currently using a (global) dictionary for this purpose-
ID = {'bag': bag_list, 'basket': basket_list}
def a(name, id):
dist_list = ID[id]
It also boils down to converting a variable name to string because the desired behavior can also be modeled as:
def a(name):
name = ???get variable name as string magically???
if name.startswith('bag'):
dist_list = ID['bag']
else:
dist_list = ID['basket']
The re-modeled question has been answered comprehensively in https://stackoverflow.com/a/9121991/1397945 but I'm re-posting this
- just in case there have been some modifications &
- whether it is possible to do this in other scripting languages like Perl or Tcl.
- More so, in case the two ways of modeling are different or can be approached any differently.
Thanks.