加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python:为什么说只引用的变量是隐式全局的?

发布时间:2020-12-16 23:30:49 所属栏目:Python 来源:网络整理
导读:从 Python FAQ开始,我们可以阅读: In Python,variables that are only referenced inside a function are implicitly global 从Python Tutorial on defining functions开始,我们可以阅读: The execution of a function introduces a new symbol table used
从 Python FAQ开始,我们可以阅读:

In Python,variables that are only referenced inside a function are implicitly global

从Python Tutorial on defining functions开始,我们可以阅读:

The execution of a function introduces a new symbol table used for the local variables of the function. More precisely,all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table,then in the local symbol tables of enclosing functions,then in the global symbol table,and finally in the table of built-in names

现在我完全理解了教程语句,但后来说只在函数内部引用的变量是隐式全局的,这对我来说似乎很模糊.

如果我们真的开始查看本地符号表,然后按照更“通用”的表格,那么为什么说它们是隐式全局的呢?它只是一种说法,如果你只想在一个函数中引用一个变量,你不需要担心它是本地的还是全局的?

解决方法

例子

(进一步了解摘要)

这意味着如果一个变量从未在函数体中赋值,那么它将被视为全局变量.

这解释了为什么以下工作(a被视为全局):

a = 1

def fn():
    print a  # This is "referencing a variable" == "reading its value"

# Prints: 1

但是,如果将变量赋值给函数体中的某个位置,则它将被视为整个函数体的局部变量.

这包括在分配之前找到的语句(请参阅下面的示例).

这解释了为什么以下不起作用.在这里,a被视为本地的,

a = 1

def fn():
    print a 
    a = 2  # <<< We're adding this

fn()

# Throws: UnboundLocalError: local variable 'a' referenced before assignment

您可以让Python使用语句global a将变量视为全局变量.如果这样做,则变量将被视为全局变量,同样适用于整个函数体.

a = 1

def fn():
   global a  # <<< We're adding this
   print a
   a = 2

fn()
print a

# Prints: 1
# Then,prints: 2 (a changed in the global scope too)

摘要

与您可能期望的不同,如果无法在本地范围内找到,Python将不会回退到全局范围.

这意味着整个函数体的变量是局部变量或全局变量:它不能是全局的,然后变为局部变量.

现在,关于变量是被视为本地变量还是全局变量,Python遵循以下规则.变量是:

>全局如果仅引用且从未分配给
>全局如果使用全局声明
>如果变量至少分配一次(并且未使用全局变量),则为Local

进一步说明

事实上,“隐含全球化”并不意味着全球化.这是一个更好的思考方式:

>“local”表示“函数内部的某个地方”
>“全球”真的意味着“在功能之外的某个地方”

因此,如果变量是“隐式全局”(==“在函数外部”),那么它的“封闭范围”将首先被查找:

a = 25

def enclosing():
   a = 2
   def enclosed():
       print a
   enclosed()

enclosing()

# Prints 2,as supplied in the enclosing scope,instead of 25 (found in the global scope)

现在,像往常一样,global允许您引用全局范围.

a = 25

def enclosing():
   a = 2
   def enclosed():
       global a  # <<< We're adding this
       print a
   enclosed()

enclosing()

# Prints 25,as supplied in the global scope

现在,如果你需要分配一个封闭的,并希望在封闭的范围内改变一个值,而不是在全局范围内,那么你需要nonlocal,这在Python 3中是新的.在Python 2中,你可以’吨.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读