import inspect
x,y,z = 1,2,3
def retrieve_name(var):
callers_local_vars = inspect.currentframe().f_back.f_locals.items()
return [var_name for var_name, var_val in callers_local_vars if var_val is var]
def mod_retrieve_name(var):
callers_local_vars = inspect.currentframe().f_back.f_back.f_locals.items()
return [var_name for var_name, var_val in callers_local_vars if var_val is var]
def foo(bar):
print retrieve_name(bar)
print mod_retrieve_name(bar)
foo(x)
aW1wb3J0IGluc3BlY3QKCngseSx6ID0gMSwyLDMKCmRlZiByZXRyaWV2ZV9uYW1lKHZhcik6CiAgICBjYWxsZXJzX2xvY2FsX3ZhcnMgPSBpbnNwZWN0LmN1cnJlbnRmcmFtZSgpLmZfYmFjay5mX2xvY2Fscy5pdGVtcygpCiAgICByZXR1cm4gW3Zhcl9uYW1lIGZvciB2YXJfbmFtZSwgdmFyX3ZhbCBpbiBjYWxsZXJzX2xvY2FsX3ZhcnMgaWYgdmFyX3ZhbCBpcyB2YXJdCiAgICAKZGVmIG1vZF9yZXRyaWV2ZV9uYW1lKHZhcik6CiAgICBjYWxsZXJzX2xvY2FsX3ZhcnMgPSBpbnNwZWN0LmN1cnJlbnRmcmFtZSgpLmZfYmFjay5mX2JhY2suZl9sb2NhbHMuaXRlbXMoKQogICAgcmV0dXJuIFt2YXJfbmFtZSBmb3IgdmFyX25hbWUsIHZhcl92YWwgaW4gY2FsbGVyc19sb2NhbF92YXJzIGlmIHZhcl92YWwgaXMgdmFyXQoKZGVmIGZvbyhiYXIpOgoJcHJpbnQgcmV0cmlldmVfbmFtZShiYXIpCglwcmludCBtb2RfcmV0cmlldmVfbmFtZShiYXIpCgpmb28oeCk=