2024-04-24 15:12:54 发布
网友
在我对Python的理解中
A = 1
变量A是对值为1的对象的引用,该值也可以被其他变量引用。你知道吗
A
1
如何查看/打印/返回引用此对象的所有变量?你知道吗
首先,得到一个dictionary of all variables currently in scope and their values。你知道吗
d = dict(globals(), **locals())
然后创建字典中所有引用的列表,其中值与您感兴趣的对象匹配:
[ref for ref in d if d[ref] is obj]
例如:
A = [1,2,3] B = A C = B d = dict(globals(), **locals()) print [ref for ref in d if d[ref] is C]
输出:
['A', 'C', 'B']
首先,得到一个dictionary of all variables currently in scope and their values。你知道吗
然后创建字典中所有引用的列表,其中值与您感兴趣的对象匹配:
例如:
输出:
相关问题 更多 >
编程相关推荐