- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
来自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
现在我完全理解了教程中的陈述,但是说仅在函数内部引用的变量是隐式全局变量
对我来说似乎很模糊。
如果我们实际上开始查看局部符号表,然后再查看更“通用”的符号表,为什么说它们是隐式全局的呢?这是否只是一种说法,如果您只打算在函数中引用一个变量,则无需担心它是本地变量还是 global
?
最佳答案
(进一步查看摘要)
这意味着如果一个变量在函数体中从未分配给,那么它将被视为全局变量。
这解释了为什么以下工作(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)
与您可能期望的不同,如果在本地范围内找不到 a
,Python 将不会回退到全局范围。
这意味着一个变量对于整个函数体来说要么是局部的要么是全局的:它不能是全局的然后变成局部的。
现在,关于一个变量是被视为局部变量还是全局变量,Python 遵循以下规则。变量是:
global
语句则为全局global
),则为局部事实上,“隐式全局”并不真正意味着全局。这是一种更好的思考方式:
因此,如果一个变量是“隐式全局”(==“函数外部”),那么它的“封闭范围”将首先被查找:
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
现在,如果您需要在 enclosed
中分配给 a
,并且希望在 enclosed 中更改
的范围,但不在全局范围内,那么您将需要 a
的值nonlocal
,这是 Python 3 中的新功能。在 Python 2 中,您不能。
关于 python : Why is it said that variables that are only referenced are implicitly global?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23458854/
好的正则表达式大师,我有一个很长的文本,我试图在包含单词“he said”和类似变体的句子中添加引号。 例如: s = 'This should have no quotes. This one sh
我不了解可以将元组结构强制转换为函数的功能,如下所示: struct MyType(u8); let optional_mytype: Option = Some(12).map(MyType); /
来自Python FAQ ,我们可以读到: In Python, variables that are only referenced inside a function are implicitly
在带有 C# 的 Visual Studio 中,如何在这样的字符串中声明一个字符串?我看到了一些 Stack Overflow 页面,例如“Java string inside string to
由于我的代码有点太长,我想如果有人愿意帮助我并且需要代码的话,发布一个github链接会更容易:https://github.com/Pigums/Cminus-Compiler 在 cygwin 中
下面的文件是 vanilla.js,文本编辑器是 ATOM,使用终端在 nodejs 上运行。代码片段如下: 'use strict'; const Readline = require('readl
Sqlfiddle 是 http://sqlfiddle.com/#!2/7df50/4 基本上,我有 3 个表:组、成员资格、客户。 tbl.client = client_id (PK, AI),
奇怪的事情正在发生。 我有一个使用 CMake 编译的 C 语言静态库。 我反对在 Ubuntu 上构建可执行文件,但在 Snow Leopard 下,当我尝试这样做时出现 undefined sym
我正在用 Python 3 制作一个小图形界面,它应该下载一个带有 URL 的 youtube 视频。 我用了youtube_dl模块。 这是我的代码: import youtube_dl # You
我有一个遍历数据数组 plotData 的循环。在 plotData 之外,我构建了另一个名为 visiblePoints 的数组。然后我遍历 visiblePoints 来构建一个对象 thisRo
我一直在 CSS 中使用 :before 表示将内容放在选择器之前。我对此从来没有遇到过任何问题,但是,我偶然发现了一些让我感到困惑的事情,选择器的 :before 是在 inside 元素后面而不是
我正在构建一个针对 iOS7 和 iOS8 的应用。我请求用户允许发送推送通知。但出于某种原因,iOS7 和 iOS8 都没有调用我的 application:didFailToRegisterFor
package springdemo4; import org.springframework.context.support.ClassPathXmlApplicationContext; publ
我正在寻找可以用\"替换字符串中每个引号的东西。 我试过这个: local te = 'Press "start" to begin!' te = string.gsub(te,'"','\") pr
我有这段代码,我使用泛型定义了两个类。 1. 可以具有通用类型数据的部分。 2. 使用某种构建器模式并存储此类部分列表的配置。 运行此代码时会出现编译错误,我不知道为什么。我已经提到了类型。 错误:不
我正在尝试创建一个结构,它有一个使用 Write 特性的 BufWriter,这样这个结构就可以有一个缓冲的 writer,它可以是任何实现该特征:File、Stream 等。但是我的函数中存在一个问
这个问题在这里已经有了答案: Callback Method if user declines Push Notification Prompt? (11 个答案) 关闭 5 年前。 在 iOS 9
我正在尝试编写一个查询,该查询将返回 Sakila 数据库中最长的电影租赁持续时间以及租赁该电影(以及电影的标题)的客户。 首先,我编写了以下查询,该查询应该返回 film_id、inventory_
完整错误信息: Error MySQL said: Documentation Cannot connect: invalid settings. Connection for controluser
我在 VMware 上使用 xampp,操作系统是 Linux,在选择或删除或任何东西时卡住了,不知道为什么会这样。我还检查了 xampp 功能是否正在运行,即 Mysql、Apache 和 ProF
我是一名优秀的程序员,十分优秀!