fork download
  1. import inspect
  2.  
  3. x,y,z = 1,2,3
  4.  
  5. def retrieve_name(var):
  6. callers_local_vars = inspect.currentframe().f_back.f_locals.items()
  7. return [var_name for var_name, var_val in callers_local_vars if var_val is var]
  8.  
  9. def mod_retrieve_name(var):
  10. callers_local_vars = inspect.currentframe().f_back.f_back.f_locals.items()
  11. return [var_name for var_name, var_val in callers_local_vars if var_val is var]
  12.  
  13. def foo(bar):
  14. print retrieve_name(bar)
  15. print mod_retrieve_name(bar)
  16.  
  17. foo(x)
Success #stdin #stdout 0s 24072KB
stdin
Standard input is empty
stdout
['bar']
['x']