Commit 91e561aa authored by Nick Coghlan's avatar Nick Coghlan

Issue #24129: Clarify reference docs for name resolution.

This includes removing the assumption that readers will be familiar with the
name resolution scheme Python used prior to the introduction of lexical
scoping for function namespaces.

Patch by Ivan Levkivskyi.
parent 36948d78
...@@ -5,29 +5,18 @@ ...@@ -5,29 +5,18 @@
Execution model Execution model
*************** ***************
.. index:: single: execution model
.. _naming:
Naming and binding
==================
.. index:: .. index::
single: execution model
pair: code; block pair: code; block
single: namespace
single: scope
.. index:: .. _prog_structure:
single: name
pair: binding; name
:dfn:`Names` refer to objects. Names are introduced by name binding operations. Structure of a programm
Each occurrence of a name in the program text refers to the :dfn:`binding` of =======================
that name established in the innermost function block containing the use.
.. index:: block .. index:: block
A Python program is constructed from code blocks.
A :dfn:`block` is a piece of Python program text that is executed as a unit. A :dfn:`block` is a piece of Python program text that is executed as a unit.
The following are blocks: a module, a function body, and a class definition. The following are blocks: a module, a function body, and a class definition.
Each command typed interactively is a block. A script file (a file given as Each command typed interactively is a block. A script file (a file given as
...@@ -43,43 +32,25 @@ A code block is executed in an :dfn:`execution frame`. A frame contains some ...@@ -43,43 +32,25 @@ A code block is executed in an :dfn:`execution frame`. A frame contains some
administrative information (used for debugging) and determines where and how administrative information (used for debugging) and determines where and how
execution continues after the code block's execution has completed. execution continues after the code block's execution has completed.
.. index:: scope .. _naming:
A :dfn:`scope` defines the visibility of a name within a block. If a local
variable is defined in a block, its scope includes that block. If the
definition occurs in a function block, the scope extends to any blocks contained
within the defining one, unless a contained block introduces a different binding
for the name. The scope of names defined in a class block is limited to the
class block; it does not extend to the code blocks of methods -- this includes
comprehensions and generator expressions since they are implemented using a
function scope. This means that the following will fail::
class A:
a = 42
b = list(a + i for i in range(10))
.. index:: single: environment Naming and binding
==================
When a name is used in a code block, it is resolved using the nearest enclosing .. index::
scope. The set of all such scopes visible to a code block is called the block's single: namespace
:dfn:`environment`. single: scope
.. index:: pair: free; variable .. _bind_names:
If a name is bound in a block, it is a local variable of that block, unless Binding of names
declared as :keyword:`nonlocal`. If a name is bound at the module level, it is ----------------
a global variable. (The variables of the module code block are local and
global.) If a variable is used in a code block but not defined there, it is a
:dfn:`free variable`.
.. index:: .. index::
single: NameError (built-in exception) single: name
single: UnboundLocalError pair: binding; name
When a name is not found at all, a :exc:`NameError` exception is raised. If the :dfn:`Names` refer to objects. Names are introduced by name binding operations.
name refers to a local variable that has not been bound, an
:exc:`UnboundLocalError` exception is raised. :exc:`UnboundLocalError` is a
subclass of :exc:`NameError`.
.. index:: statement: from .. index:: statement: from
...@@ -99,6 +70,46 @@ this purpose (though the actual semantics are to unbind the name). ...@@ -99,6 +70,46 @@ this purpose (though the actual semantics are to unbind the name).
Each assignment or import statement occurs within a block defined by a class or Each assignment or import statement occurs within a block defined by a class or
function definition or at the module level (the top-level code block). function definition or at the module level (the top-level code block).
.. index:: pair: free; variable
If a name is bound in a block, it is a local variable of that block, unless
declared as :keyword:`nonlocal` or :keyword:`global`. If a name is bound at
the module level, it is a global variable. (The variables of the module code
block are local and global.) If a variable is used in a code block but not
defined there, it is a :dfn:`free variable`.
Each occurrence of a name in the program text refers to the :dfn:`binding` of
that name established by the following name resolution rules.
.. _resolve_names:
Resolution of names
-------------------
.. index:: scope
A :dfn:`scope` defines the visibility of a name within a block. If a local
variable is defined in a block, its scope includes that block. If the
definition occurs in a function block, the scope extends to any blocks contained
within the defining one, unless a contained block introduces a different binding
for the name.
.. index:: single: environment
When a name is used in a code block, it is resolved using the nearest enclosing
scope. The set of all such scopes visible to a code block is called the block's
:dfn:`environment`.
.. index::
single: NameError (built-in exception)
single: UnboundLocalError
When a name is not found at all, a :exc:`NameError` exception is raised.
If the current scope is a function scope, and the name refers to a local
variable that has not yet been bound to a value at the point where the name is
used, an :exc:`UnboundLocalError` exception is raised.
:exc:`UnboundLocalError` is a subclass of :exc:`NameError`.
If a name binding operation occurs anywhere within a code block, all uses of the If a name binding operation occurs anywhere within a code block, all uses of the
name within the block are treated as references to the current block. This can name within the block are treated as references to the current block. This can
lead to errors when a name is used within a block before it is bound. This rule lead to errors when a name is used within a block before it is bound. This rule
...@@ -115,7 +126,41 @@ global namespace is searched first. If the name is not found there, the ...@@ -115,7 +126,41 @@ global namespace is searched first. If the name is not found there, the
builtins namespace is searched. The :keyword:`global` statement must precede builtins namespace is searched. The :keyword:`global` statement must precede
all uses of the name. all uses of the name.
.. XXX document "nonlocal" semantics here The :keyword:`global` statement has the same scope as a name binding operation
in the same block. If the nearest enclosing scope for a free variable contains
a global statement, the free variable is treated as a global.
.. XXX say more about "nonlocal" semantics here
The :keyword:`nonlocal` statement causes corresponding names to refer
to previously bound variables in the nearest enclosing function scope.
:exc:`SyntaxError` is raised at compile time if the given name does not
exist in any enclosing function scope.
.. index:: module: __main__
The namespace for a module is automatically created the first time a module is
imported. The main module for a script is always called :mod:`__main__`.
Class definition blocks and arguments to :func:`exec` and :func:`eval` are
special in the context of name resolution.
A class definition is an executable statement that may use and define names.
These references follow the normal rules for name resolution with an exception
that unbound local variables are looked up in the global namespace.
The namespace of the class definition becomes the attribute dictionary of
the class. The scope of names defined in a class block is limited to the
class block; it does not extend to the code blocks of methods -- this includes
comprehensions and generator expressions since they are implemented using a
function scope. This means that the following will fail::
class A:
a = 42
b = list(a + i for i in range(10))
.. _restrict_exec:
Builtins and restricted execution
---------------------------------
.. index:: pair: restricted; execution .. index:: pair: restricted; execution
...@@ -135,26 +180,20 @@ weak form of restricted execution. ...@@ -135,26 +180,20 @@ weak form of restricted execution.
:keyword:`import` the :mod:`builtins` module and modify its :keyword:`import` the :mod:`builtins` module and modify its
attributes appropriately. attributes appropriately.
.. index:: module: __main__
The namespace for a module is automatically created the first time a module is
imported. The main module for a script is always called :mod:`__main__`.
The :keyword:`global` statement has the same scope as a name binding operation
in the same block. If the nearest enclosing scope for a free variable contains
a global statement, the free variable is treated as a global.
A class definition is an executable statement that may use and define names.
These references follow the normal rules for name resolution. The namespace of
the class definition becomes the attribute dictionary of the class. Names
defined at the class scope are not visible in methods.
.. _dynamic-features: .. _dynamic-features:
Interaction with dynamic features Interaction with dynamic features
--------------------------------- ---------------------------------
Name resolution of free variables occurs at runtime, not at compile time.
This means that the following code will print 42::
i = 10
def f():
print(i)
i = 42
f()
There are several cases where Python statements are illegal when used in There are several cases where Python statements are illegal when used in
conjunction with nested scopes that contain free variables. conjunction with nested scopes that contain free variables.
......
...@@ -813,6 +813,7 @@ Christopher Tur Lesniewski-Laas ...@@ -813,6 +813,7 @@ Christopher Tur Lesniewski-Laas
Alain Leufroy Alain Leufroy
Mark Levinson Mark Levinson
Mark Levitt Mark Levitt
Ivan Levkivskyi
William Lewis William Lewis
Akira Li Akira Li
Xuanji Li Xuanji Li
......
...@@ -412,6 +412,11 @@ C API ...@@ -412,6 +412,11 @@ C API
Documentation Documentation
------------- -------------
- Issue #24129: Clarify the reference documentation for name resolution.
This includes removing the assumption that readers will be familiar with the
name resolution scheme Python used prior to the introduction of lexical
scoping for function namespaces. Patch by Ivan Levkivskyi.
- Issue #20769: Improve reload() docs. Patch by Dorian Pula. - Issue #20769: Improve reload() docs. Patch by Dorian Pula.
- Issue #23589: Remove duplicate sentence from the FAQ. Patch by Yongzhi Pan. - Issue #23589: Remove duplicate sentence from the FAQ. Patch by Yongzhi Pan.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment