“用途"的含义和区别是什么?和“重新绑定"一个变量? [英] What are the meanings and differences between "uses" and "rebinds" a variable?
问题描述
简而言之,来自 Python:
From Python in a Nutshell:
避开全球
如果函数体只使用一个全局变量,则不要使用全局变量变量(包括改变绑定到它的对象变量,当对象是可变的时).
Never use global if the function body just uses a global variable (including mutating the object bound to that variable, when the object is mutable).
仅在以下情况下使用全局语句函数体重新绑定一个全局变量(通常通过分配给变量的名称).
Use a global statement only if the function body rebinds a global variable (generally by assigning to the variable’s name).
使用"和重新绑定"变量的含义和区别是什么?
What are the meanings and differences between "uses" and "rebinds" a variable?
改变绑定到变量的对象"是使用"还是重新绑定"变量?为什么?
Is "mutating the object bound to a variable" "uses" or "rebinds" the variable? Why?
推荐答案
Mutate"和bind"/rebind"是两个互斥的操作.变异改变了一个对象,而绑定改变了一个名字.
"Mutate" and "bind"/"rebind" are two mutually exclusive operations. Mutating changes an object, whereas binding changes a name.
这是绑定:
a = []
这是变异:
a.append(None)
使用"意味着访问绑定到一个名称的现有对象,无论是为了读取还是为了改变.
"Using" means accessing the existing object bound to a name, whether for reading or for mutation.
这篇关于“用途"的含义和区别是什么?和“重新绑定"一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!