@@ -543,18 +543,19 @@ are always available. They are listed here in alphabetical order.
543543
544544 The *expression * argument is parsed and evaluated as a Python expression
545545 (technically speaking, a condition list) using the *globals * and *locals *
546- dictionaries as global and local namespace. If the *globals * dictionary is
546+ mappings as global and local namespace. If the *globals * dictionary is
547547 present and does not contain a value for the key ``__builtins__ ``, a
548548 reference to the dictionary of the built-in module :mod: `builtins ` is
549549 inserted under that key before *expression * is parsed. That way you can
550550 control what builtins are available to the executed code by inserting your
551551 own ``__builtins__ `` dictionary into *globals * before passing it to
552- :func: `eval `. If the *locals * dictionary is omitted it defaults to the
553- *globals * dictionary. If both dictionaries are omitted, the expression is
552+ :func: `eval `. If the *locals * mapping is omitted it defaults to the
553+ *globals * dictionary. If both mappings are omitted, the expression is
554554 executed with the *globals * and *locals * in the environment where
555- :func: `eval ` is called. Note, *eval() * does not have access to the
555+ :func: `eval ` is called. Note, *eval() * will only have access to the
556556 :term: `nested scopes <nested scope> ` (non-locals) in the enclosing
557- environment.
557+ environment if they are already referenced in the scope that is calling
558+ :func: `eval ` (e.g. via a :keyword: `nonlocal ` statement).
558559
559560 Example:
560561
@@ -587,6 +588,11 @@ are always available. They are listed here in alphabetical order.
587588
588589 The *globals * and *locals * arguments can now be passed as keywords.
589590
591+ .. versionchanged :: 3.13
592+
593+ The semantics of the default *locals * namespace have been adjusted as
594+ described for the :func: `locals ` builtin.
595+
590596.. index :: pair: built-in function; exec
591597
592598.. function :: exec(source, /, globals=None, locals=None, *, closure=None)
@@ -612,9 +618,15 @@ are always available. They are listed here in alphabetical order.
612618
613619 .. note ::
614620
615- Most users should just pass a *globals * argument and never *locals *.
616- If exec gets two separate objects as *globals * and *locals *, the code
617- will be executed as if it were embedded in a class definition.
621+ When ``exec `` gets two separate objects as *globals * and *locals *, the
622+ code will be executed as if it were embedded in a class definition. This
623+ means functions and classes defined in the executed code will not be able
624+ to access variables assigned at the top level (as the "top level"
625+ variables are treated as class variables in a class definition).
626+ Passing a :class: `collections.ChainMap ` instance as *globals * allows name
627+ lookups to be chained across multiple mappings without triggering this
628+ behaviour. Values assigned to top level names in the executed code can be
629+ retrieved by passing an empty dictionary as the first entry in the chain.
618630
619631 If the *globals * dictionary does not contain a value for the key
620632 ``__builtins__ ``, a reference to the dictionary of the built-in module
@@ -635,7 +647,7 @@ are always available. They are listed here in alphabetical order.
635647 .. note ::
636648
637649 The built-in functions :func: `globals ` and :func: `locals ` return the current
638- global and local dictionary , respectively, which may be useful to pass around
650+ global and local namespace , respectively, which may be useful to pass around
639651 for use as the second and third argument to :func: `exec `.
640652
641653 .. note ::
@@ -651,6 +663,11 @@ are always available. They are listed here in alphabetical order.
651663
652664 The *globals * and *locals * arguments can now be passed as keywords.
653665
666+ .. versionchanged :: 3.13
667+
668+ The semantics of the default *locals * namespace have been adjusted as
669+ described for the :func: `locals ` builtin.
670+
654671
655672.. function :: filter(function, iterable)
656673
@@ -1056,39 +1073,51 @@ are always available. They are listed here in alphabetical order.
10561073 variable names as the keys, and their currently bound references as the
10571074 values.
10581075
1059- At module scope, as well as when using ``exec() `` or ``eval() `` with a
1060- single namespace, this function returns the same namespace as ``globals() ``.
1076+ At module scope, as well as when using :func: `exec ` or :func: `eval ` with
1077+ a single namespace, this function returns the same namespace as
1078+ :func: `globals `.
10611079
10621080 At class scope, it returns the namespace that will be passed to the
10631081 metaclass constructor.
10641082
10651083 When using ``exec() `` or ``eval() `` with separate local and global
1066- namespaces , it returns the local namespace passed in to the function call.
1084+ arguments , it returns the local namespace passed in to the function call.
10671085
10681086 In all of the above cases, each call to ``locals() `` in a given frame of
10691087 execution will return the *same * mapping object. Changes made through
1070- the mapping object returned from ``locals() `` will be visible as bound,
1071- rebound, or deleted local variables, and binding, rebinding, or deleting
1072- local variables will immediately affect the contents of the returned mapping
1073- object.
1074-
1075- At function scope (including for generators and coroutines), each call to
1076- ``locals() `` instead returns a fresh dictionary containing the current
1077- bindings of the function's local variables and any nonlocal cell references.
1078- In this case, name binding changes made via the returned dict are *not *
1079- written back to the corresponding local variables or nonlocal cell
1080- references, and binding, rebinding, or deleting local variables and nonlocal
1081- cell references does *not * affect the contents of previously returned
1082- dictionaries.
1088+ the mapping object returned from ``locals() `` will be visible as assigned,
1089+ reassigned, or deleted local variables, and assigning, reassigning, or
1090+ deleting local variables will immediately affect the contents of the
1091+ returned mapping object.
1092+
1093+ In an :term: `optimized scope ` (including functions, generators, and
1094+ coroutines), each call to ``locals() `` instead returns a fresh dictionary
1095+ containing the current bindings of the function's local variables and any
1096+ nonlocal cell references. In this case, name binding changes made via the
1097+ returned dict are *not * written back to the corresponding local variables
1098+ or nonlocal cell references, and assigning, reassigning, or deleting local
1099+ variables and nonlocal cell references does *not * affect the contents
1100+ of previously returned dictionaries.
1101+
1102+ Calling ``locals() `` as part of a comprehension in a function, generator, or
1103+ coroutine is equivalent to calling it in the containing scope, except that
1104+ the comprehension's initialised iteration variables will be included. In
1105+ other scopes, it behaves as if the comprehension were running as a nested
1106+ function.
1107+
1108+ Calling ``locals() `` as part of a generator expression is equivalent to
1109+ calling it in a nested generator function.
1110+
1111+ .. versionchanged :: 3.12
1112+ The behaviour of ``locals() `` in a comprehension has been updated as
1113+ described in :pep: `709 `.
10831114
10841115 .. versionchanged :: 3.13
1085- In previous versions, the semantics of mutating the mapping object
1086- returned from this function were formally undefined. In CPython
1087- specifically, the mapping returned at function scope could be
1088- implicitly refreshed by other operations, such as calling ``locals() ``
1089- again. Obtaining the legacy CPython behaviour now requires explicit
1090- calls to update the initially returned dictionary with the results
1091- of subsequent calls to ``locals() ``.
1116+ As part of :pep: `667 `, the semantics of mutating the mapping objects
1117+ returned from this function are now defined. The behavior in
1118+ :term: `optimized scopes <optimized scope> ` is now as described above.
1119+ Aside from being defined, the behaviour in other scopes remains
1120+ unchanged from previous versions.
10921121
10931122
10941123.. function :: map(function, iterable, *iterables)
@@ -1975,14 +2004,18 @@ are always available. They are listed here in alphabetical order.
19752004 :attr: `~object.__dict__ ` attributes (for example, classes use a
19762005 :class: `types.MappingProxyType ` to prevent direct dictionary updates).
19772006
1978- Without an argument, :func: `vars ` acts like :func: `locals `. Note, the
1979- locals dictionary is only useful for reads since updates to the locals
1980- dictionary are ignored.
2007+ Without an argument, :func: `vars ` acts like :func: `locals `.
19812008
19822009 A :exc: `TypeError ` exception is raised if an object is specified but
19832010 it doesn't have a :attr: `~object.__dict__ ` attribute (for example, if
19842011 its class defines the :attr: `~object.__slots__ ` attribute).
19852012
2013+ .. versionchanged :: 3.13
2014+
2015+ The result of calling this function without an argument has been
2016+ updated as described for the :func: `locals ` builtin.
2017+
2018+
19862019.. function :: zip(*iterables, strict=False)
19872020
19882021 Iterate over several iterables in parallel, producing tuples with an item
0 commit comments