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.
question from:
https://stackoverflow.com/questions/1538342/how-can-i-get-the-name-of-an-object-in-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…