• <small id='gH4Pk'></small><noframes id='gH4Pk'>

    <legend id='gH4Pk'><style id='gH4Pk'><dir id='gH4Pk'><q id='gH4Pk'></q></dir></style></legend>

    <tfoot id='gH4Pk'></tfoot>
    <i id='gH4Pk'><tr id='gH4Pk'><dt id='gH4Pk'><q id='gH4Pk'><span id='gH4Pk'><b id='gH4Pk'><form id='gH4Pk'><ins id='gH4Pk'></ins><ul id='gH4Pk'></ul><sub id='gH4Pk'></sub></form><legend id='gH4Pk'></legend><bdo id='gH4Pk'><pre id='gH4Pk'><center id='gH4Pk'></center></pre></bdo></b><th id='gH4Pk'></th></span></q></dt></tr></i><div id='gH4Pk'><tfoot id='gH4Pk'></tfoot><dl id='gH4Pk'><fieldset id='gH4Pk'></fieldset></dl></div>
        • <bdo id='gH4Pk'></bdo><ul id='gH4Pk'></ul>

        未绑定的变量和名称

        时间:2023-09-10
      1. <legend id='4G8Pv'><style id='4G8Pv'><dir id='4G8Pv'><q id='4G8Pv'></q></dir></style></legend>
        <tfoot id='4G8Pv'></tfoot>

          <small id='4G8Pv'></small><noframes id='4G8Pv'>

              <bdo id='4G8Pv'></bdo><ul id='4G8Pv'></ul>

                  <tbody id='4G8Pv'></tbody>

                1. <i id='4G8Pv'><tr id='4G8Pv'><dt id='4G8Pv'><q id='4G8Pv'><span id='4G8Pv'><b id='4G8Pv'><form id='4G8Pv'><ins id='4G8Pv'></ins><ul id='4G8Pv'></ul><sub id='4G8Pv'></sub></form><legend id='4G8Pv'></legend><bdo id='4G8Pv'><pre id='4G8Pv'><center id='4G8Pv'></center></pre></bdo></b><th id='4G8Pv'></th></span></q></dt></tr></i><div id='4G8Pv'><tfoot id='4G8Pv'></tfoot><dl id='4G8Pv'><fieldset id='4G8Pv'></fieldset></dl></div>
                2. 本文介绍了未绑定的变量和名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  根据python参考手册我们有

                  当根本找不到名称时,会引发 NameError 异常.如果该名称指的是尚未绑定的局部变量,a引发 UnboundLocalError 异常.UnboundLocalError 是一个子类的名称错误.

                  When a name is not found at all, a NameError exception is raised. If the name refers to a local variable that has not been bound, a UnboundLocalError exception is raised. UnboundLocalError is a subclass of NameError.

                  我不明白什么时候抛出 UnboundLocalError?因为

                  I don't to understand when the UnboundLocalError is thrown? Because

                  Python 缺少声明并允许发生名称绑定操作代码块内的任何位置.

                  Python lacks declarations and allows name binding operations to occur anywhere within a code block.

                  那么我们怎样才能声明一个变量,而不是初始化她呢?

                  So how we can declare a variable, but not to initialize her?

                  推荐答案

                  你可以引用一个没有分配的名字:

                  You can refer to a name without having assigned to it:

                  >>> foobar
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                  NameError: name 'foobar' is not defined
                  

                  这里 foobar 被引用,但从未被分配到.这会引发 NameError 因为名称从未绑定.

                  Here foobar is being referred to, but was never assigned to. This raises a NameError because the name was never bound.

                  更微妙的是,这里没有发生分配,因为执行的行永远不会运行:

                  More subtly, here assignment is not happening because the line that does is never run:

                  >>> def foo():
                  ...     if False:
                  ...         spam = 'eggs'
                  ...     print spam
                  ... 
                  >>> foo()
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "<stdin>", line 4, in foo
                  UnboundLocalError: local variable 'spam' referenced before assignment
                  

                  因为 spam = 'eggs' 永远不会执行,所以 print spam 会引发 UnboudLocalError.

                  Because spam = 'eggs' is never executed, print spam raises an UnboudLocalError.

                  请注意,Python 中没有任何地方声明过.您绑定或不绑定,声明不是语言的一部分.

                  Note that nowhere in Python is a name ever declared. You bind or don't bind, declaration is not part of the language.

                  相反,binding 用于确定名称的范围;绑定操作包括赋值、用于 for 循环的名称、函数参数、导入语句、在 except 子句中保存捕获的异常的名称、上下文管理器的名称一个 with 语句所有的绑定名称.

                  Instead, binding is used to determine the scope of a name; binding operations include assignment, names used for a for loop, function parameters, import statements, name to hold a caught exception in an except clause, the name for a context manager in a with statement all bind names.

                  如果名称绑定在范围内(例如在函数中),则它是本地名称,除非您使用 global 语句(或 nonlocal 语句在 Python 3 中)将名称显式标记为全局(或闭包).

                  If a name is bound in a scope (such as in a function) then it is a local name, unless you use a global statement (or a nonlocal statement in Python 3) to explicitly mark the name as a global (or a closure) instead.

                  所以下面是一个错误:

                  >>> foo = None
                  >>> def bar():
                  ...     if False:
                  ...         foo = 'spam'
                  ...     print foo
                  ... 
                  >>> bar()
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "<stdin>", line 4, in bar
                  UnboundLocalError: local variable 'foo' referenced before assignment
                  

                  因为 foosomewhere 绑定在 bar 函数范围内.但是如果您将 foo 标记为全局,则该函数可以工作:

                  because foo is being bound somewhere in the bar function scope. But if you mark foo as a global, the function works:

                  >>> foo = None
                  >>> def bar():
                  ...     global foo
                  ...     if False:
                  ...         foo = 'spam'
                  ...     print foo
                  ... 
                  >>> bar()
                  None
                  

                  因为现在 Python 编译器知道您希望 foo 改为全局变量.

                  because now the Python compiler knows you wanted foo to be a global instead.

                  所有这些都记录在 命名和绑定部分 Python 参考文档.

                  This is all documented in the Naming and Binding section of the Python reference documentation.

                  这篇关于未绑定的变量和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:`except` 子句中的名称绑定在子句后删除 下一篇:python函数可以调用同名的全局函数吗?

                  相关文章

                  最新文章

                3. <legend id='DL2bp'><style id='DL2bp'><dir id='DL2bp'><q id='DL2bp'></q></dir></style></legend>
                    <bdo id='DL2bp'></bdo><ul id='DL2bp'></ul>
                  <i id='DL2bp'><tr id='DL2bp'><dt id='DL2bp'><q id='DL2bp'><span id='DL2bp'><b id='DL2bp'><form id='DL2bp'><ins id='DL2bp'></ins><ul id='DL2bp'></ul><sub id='DL2bp'></sub></form><legend id='DL2bp'></legend><bdo id='DL2bp'><pre id='DL2bp'><center id='DL2bp'></center></pre></bdo></b><th id='DL2bp'></th></span></q></dt></tr></i><div id='DL2bp'><tfoot id='DL2bp'></tfoot><dl id='DL2bp'><fieldset id='DL2bp'></fieldset></dl></div>
                  1. <small id='DL2bp'></small><noframes id='DL2bp'>

                      <tfoot id='DL2bp'></tfoot>