您可以使用locals()
重要的是要注意,这应该最终作为方法的第一步。如果在方法中引入新变量,则将更改结果。因此,请确保按照以下方式进行操作:
def a(a, b, c): # make this your first statement print(len(locals()))
如果您这样做:
def a(a, b, c): z = 5 print(len(locals()))
您最终将得到4,这与您的预期结果不符。
有关本地人的文档()

您可以使用locals()
重要的是要注意,这应该最终作为方法的第一步。如果在方法中引入新变量,则将更改结果。因此,请确保按照以下方式进行操作:
def a(a, b, c): # make this your first statement print(len(locals()))
如果您这样做:
def a(a, b, c): z = 5 print(len(locals()))
您最终将得到4,这与您的预期结果不符。
有关本地人的文档()