如何在python中获得函数内部定义的所有局部变量?

如何在python中获得函数内部定义的所有局部变量?,python,function,local-variables,Python,Function,Local Variables,是否有任何方法可以打印所有的局部变量而不必打印它们 def some_function(a,b): name='mike' city='new york' #here print all the local variables inside this function? 这将是内置的功能 Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on wi

是否有任何方法可以打印所有的局部变量而不必打印它们

def some_function(a,b):
    name='mike'
    city='new york'

    #here print all the local variables inside this function?
这将是内置的功能

Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>}
>>> x = 5
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'x': 5}

这回答了你的问题吗@Countour积分不,我的问题是关于函数变量的。下面的答案满足了我的问题。第一个答案和至少一半的答案提到了如何获取局部变量,尽管标题更一般化。它确实回答了你的问题。
>>> [_ for _ in locals() if not (_.startswith('__') and _.endswith('__'))]
['x']