Is there any way to get the name of an object in Python? For instance:
my_list = [x, y, z] # x, y, z have been previously defined
for bla in my_list:
print "handling object ", name(bla) # <--- what would go instead of `name`?
# do something to bla
Edit: Some context:
What I'm actually doing is creating a list of functions that I can specify by the command line.
I have:
def fun1:
pass
def fun2
pass
def fun3:
pass
fun_dict = {'fun1': fun1,
'fun2': fun2,
'fun3': fun3}
I get the name of the function from the commandline and I want to call the relevant function:
func_name = parse_commandline()
fun_dict[func_name]()
And the reason I want to have the name of the function is because I want to create fun_dict
without writing the names of the functions twice, since that seems like a good way to create bugs. What I want to do is:
fun_list = [fun1, fun2, fun3] # and I'll add more as the need arises
fun_dict = {}
[fun_dict[name(t) = t for t in fun_list] # <-- this is where I need the name function
This way I only need to write the function names once.