有没有办法让Python打印所有绑定变量的名称和值?
(无需重新设计程序将它们全部存储在一个列表中)
答案 0 :(得分:2)
答案 1 :(得分:2)
是的,你可以,这是一种相当肮脏的方式,但它有利于调试等
from pprint import pprint
def getCurrentVariableState():
pprint(locals())
pprint(globals())
答案 2 :(得分:0)
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
答案 3 :(得分:0)
globals()和locals()分别返回表示全局和局部范围中符号表的字典。