简体   繁体   English

为什么 Python 不允许将全局变量分配给具有相同名称的本地变量?

[英]Why doesn't Python allow assigning a global variable to a local one with the same name?

I understand the logic behind requiring global if a global variable is assigned to within a function, but I don't understand why this can't be done:如果在函数内分配全局变量,我理解需要global背后的逻辑,但我不明白为什么不能这样做:

y = 0
def x():
    y = y
    # Now modify the local version, but not the global one

Calling x results in an UnboundLocalError , even though there's no ambiguity here: The right-hand side of the assignment should be evaluated to the global variable since y is undefined in a local scope when it is executed.调用x导致UnboundLocalError ,即使这里没有歧义:赋值的右侧应该评估为全局变量,因为y在执行时在局部范围内未定义。

This question is similar to this one (which doesn't have any detailed answers), but is more about the reasoning behind the language design.这个问题类似于这一个(不具有任何详细解答),但更多的是语言设计背后的原因。

I wouldn't write code like that because it is confusing, but I don't see why the language would forbid it.我不会写那样的代码,因为它令人困惑,但我不明白为什么语言会禁止它。 It seems inconsistent with how globals (or for that matter nonlocal s) usually behave in python.这似乎与全局变量(或就此而言nonlocal变量)在 python 中的通常行为方式不一致。

When a variable name is used on the left-hand side of an assignment statement Python creates a local variable.当在赋值语句的左侧使用变量名时,Python 会创建一个局部变量。 When a local variable has the same name as a global variable we say that the local shadows the global .当一个局部变量具有相同的名称作为全局变量,我们说,局部的阴影全球 A shadow means that the global variable cannot be accessed by Python because the local variable will be found first.影子意味着 Python 无法访问全局变量,因为将首先找到局部变量。

you're using an assignment statement on y inside the function.您在函数内的 y 上使用赋值语句。

The Python interpreter sees this at module load time and decides that the global scope's y should not be used inside the local scope, which leads to a problem when you try to reference the variable before it is locally assigned. Python 解释器在模块加载时看到这一点,并决定全局范围的 y 不应在局部范围内使用,这会导致在本地分配变量之前尝试引用该变量时出现问题。

that is the reason you would need to add这就是你需要添加的原因

y=0
def x():
    global y
    y=y
x()

This will tell Python that you don't intend to define ay variable inside the function's local scope.这将告诉 Python 您不打算在函数的局部作用域内定义 y 变量。 The Python interpreter sees this at module load time and decides to look up any references to the aforementioned variables in the global scope. Python 解释器在模块加载时看到这一点,并决定在全局范围内查找对上述变量的任何引用。

Though it is one of several good reasons for not using global variables.尽管这是不使用全局变量的几个很好的原因之一。 As you can see, it makes your code confusing and difficult to understand, one should refrain from doing this in code.如您所见,它使您的代码混乱且难以理解,应避免在代码中执行此操作。 I suggested adding a global keyword just to cement the understanding of how python is working here.我建议添加一个global 关键字只是为了巩固对 python 在这里工作方式的理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分配与全局变量同名的局部变量时出错 - Error when assigning local variable with same name as a global variable 在 Python 中引用与局部变量同名的全局变量 - Refer a global variable that has same name as the local variable in Python 为具有相同名称的全局变量分配参数 - Assigning an argument to a global variable with the same name 为什么Python lint要求我使用不同的局部变量名而不是全局变量名 - why does Python lint want me to use different local variable name, than a global, for the same purpose Python:名称“变量”是本地的和全局的 - Python: name 'variable' is local and global python为什么不能在函数中对相同变量使用全局和局部作用域 - why can't python use global and local scope for same variable in a function 修改与本地变量同名的全局变量 - Modifying global variable with same name as local variable 在python中为局部变量赋值然后访问全局变量 - Assigning value to local variable then accessing the global variable in python 为什么Python不允许在同一行上放置一个for后跟一个if? - Why doesn't Python allow to put a for followed by an if on the same line? 为什么分配给与本地变量同名的类属性会引发NameError? - Why does assigning to a class attribute with the same name as a local variable raise a NameError?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM