@@ -543,18 +543,19 @@ are always available. They are listed here in alphabetical order.
543
543
544
544
The *expression * argument is parsed and evaluated as a Python expression
545
545
(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
547
547
present and does not contain a value for the key ``__builtins__ ``, a
548
548
reference to the dictionary of the built-in module :mod: `builtins ` is
549
549
inserted under that key before *expression * is parsed. That way you can
550
550
control what builtins are available to the executed code by inserting your
551
551
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
554
554
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
556
556
: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).
558
559
559
560
Example:
560
561
@@ -587,6 +588,11 @@ are always available. They are listed here in alphabetical order.
587
588
588
589
The *globals * and *locals * arguments can now be passed as keywords.
589
590
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
+
590
596
.. index :: pair: built-in function; exec
591
597
592
598
.. function :: exec(source, /, globals=None, locals=None, *, closure=None)
@@ -612,9 +618,15 @@ are always available. They are listed here in alphabetical order.
612
618
613
619
.. note ::
614
620
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.
618
630
619
631
If the *globals * dictionary does not contain a value for the key
620
632
``__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.
635
647
.. note ::
636
648
637
649
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
639
651
for use as the second and third argument to :func: `exec `.
640
652
641
653
.. note ::
@@ -651,6 +663,11 @@ are always available. They are listed here in alphabetical order.
651
663
652
664
The *globals * and *locals * arguments can now be passed as keywords.
653
665
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
+
654
671
655
672
.. function :: filter(function, iterable)
656
673
@@ -1056,39 +1073,51 @@ are always available. They are listed here in alphabetical order.
1056
1073
variable names as the keys, and their currently bound references as the
1057
1074
values.
1058
1075
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 `.
1061
1079
1062
1080
At class scope, it returns the namespace that will be passed to the
1063
1081
metaclass constructor.
1064
1082
1065
1083
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.
1067
1085
1068
1086
In all of the above cases, each call to ``locals() `` in a given frame of
1069
1087
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 `.
1083
1114
1084
1115
.. 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.
1092
1121
1093
1122
1094
1123
.. function :: map(function, iterable, *iterables)
@@ -1975,14 +2004,18 @@ are always available. They are listed here in alphabetical order.
1975
2004
:attr: `~object.__dict__ ` attributes (for example, classes use a
1976
2005
:class: `types.MappingProxyType ` to prevent direct dictionary updates).
1977
2006
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 `.
1981
2008
1982
2009
A :exc: `TypeError ` exception is raised if an object is specified but
1983
2010
it doesn't have a :attr: `~object.__dict__ ` attribute (for example, if
1984
2011
its class defines the :attr: `~object.__slots__ ` attribute).
1985
2012
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
+
1986
2019
.. function :: zip(*iterables, strict=False)
1987
2020
1988
2021
Iterate over several iterables in parallel, producing tuples with an item
0 commit comments