Python,打印所有绑定变量的名称和值

Python,打印所有绑定变量的名称和值,python,Python,有没有办法让Python打印所有绑定变量的名称和值 (无需重新设计程序以将它们全部存储在一个列表中)并应提供您所需的内容。和locals()分别返回一个表示全局和局部范围内符号表的字典。是的,您可以这样做,这是一种相当肮脏的方式,但它有利于调试等 dir(...) dir([object]) -> list of strings If called without an argument, return the names in the current scope.

有没有办法让Python打印所有绑定变量的名称和值


(无需重新设计程序以将它们全部存储在一个列表中)

并应提供您所需的内容。

和locals()分别返回一个表示全局和局部范围内符号表的字典。

是的,您可以这样做,这是一种相当肮脏的方式,但它有利于调试等

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.
from pprint import pprint

def getCurrentVariableState():
    pprint(locals())
    pprint(globals())

这甚至不接近所有的界限variables@aaron是的,当然,这是四个望远镜中的两个。缺少内置的和非本地的。