globals()
и
locals()
в помощь:
>>> a = 123
>>> b = 'asd'
>>> def test():
... c = True
... d = 55.36
... print('GLOBALS:')
... pprint(globals())
... print('LOCALS:')
... pprint(locals())
...
>>> from pprint import pprint
>>> test()
GLOBALS:
{'__annotations__': {},
'__builtins__': <module 'builtins' (built-in)>,
'__doc__': None,
'__loader__': <class '_frozen_importlib.BuiltinImporter'>,
'__name__': '__main__',
'__package__': None,
'__spec__': None,
'a': 123,
'b': 'asd',
'pprint': <function pprint at 0x103ce9158>,
'test': <function test at 0x101d62e18>}
LOCALS:
{'c': True, 'd': 55.36}
>>>