简体   繁体   English

python中的全局和局部作用域

[英]Global and local scope in python

Just a beginner question about local and global scope in python 只是有关python本地和全局范围的初学者问题

X = 100
#is X a global variable?.We defined it outside the function scope
def foo():
    print(X)
    return X
#it prints 100 and even returns it
def foo():
    X = X + 10
#local error
#UnboundLocalError: local variable 'X' referenced before assignment
def foo():
    global X
    # if X is a global variable why specify again?
    X = X + 10
    return X

要修改变量的全局副本,您需要使用global关键字,但如果仅访问它,则不需要global

from the python website : python网站

In Python, variables that are only referenced inside a function are implicitly global. 在Python中,仅在函数内部引用的变量是隐式全局的。 If a variable is assigned a value anywhere within the function's body, it's assumed to be a local unless explicitly declared as global. 如果在函数体内任何位置为变量分配了值,除非明确声明为全局变量,否则将假定该变量为局部变量。

this means that you can access a global variable inside a function without a global keyword. 这意味着您可以在没有global关键字的情况下访问函数内部的全局变量。 if you want to change it though, you must use the global keyword beforehand. 如果要更改它,则必须事先使用global关键字。

global and nonlocal are very strange things when I was a beginner. 当我还是一个初学者时, globalnonlocal都是非常奇怪的事情。

Just think about it: why do we need them in Python? 试想一下:为什么我们在Python中需要它们?

It is because we don't need var , let and such similar things to declare variables. 这是因为我们不需要varlet和类似的东西来声明变量。

Think about Javascript , it is dynamic script language too and very alike to python, but it needs var or let or const to declare variables. 考虑一下Javascript ,它也是动态脚本语言,与python非常相似,但是它需要varletconst来声明变量。

The most important thing of declaring variables is to determine scope. 声明变量最重要的是确定范围。

So, in Python, our variables have implicit default scope: current scope where they are defined, and if we want to change scope of some variables, we need use global or nonlocal explicitly . 因此,在Python中,变量具有隐式的默认范围:定义变量的当前范围,如果要更改某些变量的范围,则需要显式使用globalnonlocal

All names on the left side of = mean to define variables. =左侧的所有名称均表示定义变量。

Before executing code of some certain scope, Python will pre-compute all local variables , which are those on the left side of = . 在执行一定范围的代码之前,Python将预先计算所有local variables ,这些local variables位于=的左侧。 This is why you got UnboundLocalError: local variable 'X' referenced before assignment in: 这就是为什么UnboundLocalError: local variable 'X' referenced before assignment以下内容UnboundLocalError: local variable 'X' referenced before assignment的原因:

def foo():
    X = X + 10

So, if we look up those names not in defined current scope, just follow the rules of scope chain: up, up, up and until built_in . 因此,如果我们查找不在当前定义的范围内的那些名称,则只需遵循范围链的规则:up,up,up直到built_in

Remember : scope of any name on the left side of = is default current scope, and you have to assign it(bind something to it) before referring it. 请记住=左侧的任何名称的范围都是默认的当前范围,在引用它之前必须先对其进行分配(将其绑定)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM