英文:
Where is a function array parameter stored between subsequent calls in Python?
问题
以下是翻译好的部分:
"我想了解Python如何在第一次调用和第二次调用函数之间保持对数组值的引用。是否有一个符号表可以查询或查看以查看第一次和第二次调用之间的对象值。"
def scopeUnderstanding2(a, myArray=[]):
print(f"函数scopeUnderstanding2中的myArray:{myArray}")
myArray.append(a)
print(f"在执行.append(a)后的函数scopeUnderstanding2中的myArray:{myArray}")
print(scopeUnderstanding2(1))
print(scopeUnderstanding2(2))
"函数scopeUnderstanding2中的myArray:[]"
"在执行.append(a)后的函数scopeUnderstanding2中的myArray:[1]"
"函数scopeUnderstanding2中的myArray:[1]"
"在执行.append(a)后的函数scopeUnderstanding2中的myArray:[1, 2]"
英文:
I am wanting to understand how Python maintains its reference to the array value between the first call and second call to the function. Is there a symbolic table that can be queried or looked at to see what the object values are between the first and second call.
def scopeUnderstanding2(a, myArray=[]):
print(f"myArray in func scopeUnderstanding2 myArray : {myArray}")
myArray.append(a)
print(f"myArray in func after .append(a) myArray : {myArray}")
print (scopeUnderstanding2(1))
print (scopeUnderstanding2(2))
myArray in func scopeUnderstanding2 myArray : []
myArray in func after .append(a) myArray : [1]
None
myArray in func scopeUnderstanding2 myArray : [1]
myArray in func after .append(a) myArray : [1, 2]
None
答案1
得分: 1
任何你在函数的def
语句中定义的默认参数都会存储在生成的函数对象中,存储在名为__defaults__
的属性中:
>>> def scopeUnderstanding2(a, myArray=[]):
... print(f"myArray in func scopeUnderstanding2 myArray : {myArray}")
... myArray.append(a)
... print(f"myArray in func after .append(a) myArray : {myArray}")
...
>>> dir(scopeUnderstanding2)
['__annotations__', '__builtins__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> scopeUnderstanding2.__defaults__
([],)
>>> scopeUnderstanding2(1)
myArray in func scopeUnderstanding2 myArray : []
myArray in func after .append(a) myArray : [1]
>>> scopeUnderstanding2.__defaults__
([1],)
如果myArray
被声明为函数体内的本地变量,它将在每次函数调用时重新创建,而不是与函数对象永久关联。
英文:
Any default parameters that you define in the function's def
statement are stored as part of the resulting function object in an attribute called __defaults__
:
>>> def scopeUnderstanding2(a, myArray=[]):
... print(f"myArray in func scopeUnderstanding2 myArray : {myArray}")
... myArray.append(a)
... print(f"myArray in func after .append(a) myArray : {myArray}")
...
>>> dir(scopeUnderstanding2)
['__annotations__', '__builtins__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> scopeUnderstanding2.__defaults__
([],)
>>> scopeUnderstanding2(1)
myArray in func scopeUnderstanding2 myArray : []
myArray in func after .append(a) myArray : [1]
>>> scopeUnderstanding2.__defaults__
([1],)
If myArray
had been declared as a local variable inside the body of the function, it would be re-created each time the function is called, rather than associated permanently with the function object itself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论