您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

为什么Python中的函数可以在封闭范围内打印变量,但不能在赋值中使用它们?

为什么Python中的函数可以在封闭范围内打印变量,但不能在赋值中使用它们?

类和函数是不同的,类内部的变量实际上是作为其属性分配给类的名称间的,而在函数内部,变量只是不能在其外部访问的普通变量。

实际上,函数内部的局部变量是在首次解析该函数??时确定的,并且python不会在全局范围内搜索它们,因为它知道您已将其声明为局部变量。

因此,一旦python看到x = x + 1(赋值)并且没有global为该变量声明,那么python将不会在全局或其他范围内寻找该变量。

>>> x = 'outer'
>>> def func():
...     x = 'inner'  #x is a local variable Now
...     print x
...     
>>> func()
inner

>>> x = 'outer'
>>> def func():
...     print x       #this won't access the global `x`
...     x = 'inner'   #`x` is a local variable
...     print x
...     
>>> func()
...
UnboundLocalError: local variable 'x' referenced before assignment

但是,当您使用一条global语句时,则使用python在global范围内查找该变量。

阅读:当变量具有值时,为什么会收到UnboundLocalError?

:对于嵌套函数,您可以使用nonlocalpy3.x中的语句修改在封闭函数中声明的变量。

但是类的工作方式不同,实际上x在类内部声明的变量A变为A.x

>>> x = 'outer'
>>> class A:
...    x += 'inside'  #use the value of global `x` to create a new attribute `A.x`
...    print x        #prints `A.x`
...     
outerinside
>>> print x
outer

您还可以直接从全局范围访问类属性

>>> A.x
'outerinside'

global在课堂上使用:

>>> x = 'outer'
>>> class A:
...     global x
...     x += 'inner' #Now x is not a class attribute, you just modified the global x
...     print x
...     
outerinner
>>> x
'outerinner'
>>> A.x
AttributeError: class A has no attribute 'x'

函数的陷阱不会在类中引发错误

>>> x = 'outer'
>>> class A:
...     print x                      #fetch from globals or builitns
...     x = 'I am a class attribute' #declare a class attribute
...     print x                      #print class attribute, i.e `A.x`
...     
outer
I am a class attribute
>>> x
'outer'
>>> A.x
'I am a class attribute'

规则:如果没有globalnonlocal被使用,则蟒蛇搜索顺序。

>>> outer = 'global'
>>> def func():
        enclosing = 'enclosing'
        def inner():
                inner = 'inner'
                print inner           #fetch from (L)ocal scope
                print enclosing       #fetch from (E)nclosing scope
                print outer           #fetch from (G)lobal scope
                print any             #fetch from (B)uilt-ins
        inner()
...         
>>> func()
inner
enclosing
global
<built-in function any>
python 2022/1/1 18:25:20 有401人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶