如何在解释器 python 上查看所有变量? (命令是什么?)

How to see all variable on interpreter python? (what is the command?)

是否有查看我定义的所有变量的命令?

示例:

     toto = 1
     tutu = 2
     tata = 3

假设我记得我有toto,但我忘记了其他人的声明(tutu和tata)。是否有打印我所有变量的命令?

您可以使用 globals() 查看当前文件中所有可访问的 variables/functions:

>>> toto = 1
>>> tutu = 2
>>> tata = 3
>>> globals()
{
     '__builtins__': <module '__builtin__' (built-in)>, 
     'tata': 3, 
     'toto': 1, 
     '__package__': None, 
     '__name__': '__main__', 
     'tutu': 2, 
     '__doc__': None
}
>>> toto = 1
>>> tutu = 2
>>> tata = 3
>>> from pprint import pprint
>>> pprint(globals())
{'__builtins__': <module 'builtins' (built-in)>,
 '__doc__': None,
 '__loader__': <class '_frozen_importlib.BuiltinImporter'>,
 '__name__': '__main__',
 '__package__': None,
 '__spec__': None,
 'pprint': <function pprint at 0x7f59687bdc80>,
 'tata': 3,
 'toto': 1,
 'tutu': 2}
>>> help(globals)
Help on built-in function globals in module builtins:

globals()
    Return the dictionary containing the current scope's global variables.

    NOTE: Updates to this dictionary *will* affect name lookups in the current
    global scope and vice-versa.
(END)