@@ -636,8 +636,7 @@ are always available. They are listed here in alphabetical order.
636
636
637
637
.. note ::
638
638
639
- The default *locals * act as described for function :func: `locals ` below:
640
- modifications to the default *locals * dictionary should not be attempted.
639
+ The default *locals * act as described for function :func: `locals ` below.
641
640
Pass an explicit *locals * dictionary if you need to see effects of the
642
641
code on *locals * after function :func: `exec ` returns.
643
642
@@ -1049,14 +1048,44 @@ are always available. They are listed here in alphabetical order.
1049
1048
1050
1049
.. function :: locals()
1051
1050
1052
- Update and return a dictionary representing the current local symbol table.
1053
- Free variables are returned by :func: `locals ` when it is called in function
1054
- blocks, but not in class blocks. Note that at the module level, :func: `locals `
1055
- and :func: `globals ` are the same dictionary.
1051
+ Return a mapping object representing the current local symbol table, with
1052
+ variable names as the keys, and their currently bound references as the
1053
+ values.
1054
+
1055
+ At module scope, as well as when using ``exec() `` or ``eval() `` with a
1056
+ single namespace, this function returns the same namespace as ``globals() ``.
1057
+
1058
+ At class scope, it returns the namespace that will be passed to the
1059
+ metaclass constructor.
1060
+
1061
+ When using ``exec() `` or ``eval() `` with separate local and global
1062
+ namespaces, it returns the local namespace passed in to the function call.
1063
+
1064
+ In all of the above cases, each call to ``locals() `` in a given frame of
1065
+ execution will return the *same * mapping object. Changes made through
1066
+ the mapping object returned from ``locals() `` will be visible as bound,
1067
+ rebound, or deleted local variables, and binding, rebinding, or deleting
1068
+ local variables will immediately affect the contents of the returned mapping
1069
+ object.
1070
+
1071
+ At function scope (including for generators and coroutines), each call to
1072
+ ``locals() `` instead returns a fresh dictionary containing the current
1073
+ bindings of the function's local variables and any nonlocal cell references.
1074
+ In this case, name binding changes made via the returned dict are *not *
1075
+ written back to the corresponding local variables or nonlocal cell
1076
+ references, and binding, rebinding, or deleting local variables and nonlocal
1077
+ cell references does *not * affect the contents of previously returned
1078
+ dictionaries.
1079
+
1080
+ .. versionchanged :: 3.13
1081
+ In previous versions, the semantics of mutating the mapping object
1082
+ returned from this function were formally undefined. In CPython
1083
+ specifically, the mapping returned at function scope could be
1084
+ implicitly refreshed by other operations, such as calling ``locals() ``
1085
+ again. Obtaining the legacy CPython behaviour now requires explicit
1086
+ calls to update the initially returned dictionary with the results
1087
+ of subsequent calls to ``locals() ``.
1056
1088
1057
- .. note ::
1058
- The contents of this dictionary should not be modified; changes may not
1059
- affect the values of local and free variables used by the interpreter.
1060
1089
1061
1090
.. function :: map(function, iterable, *iterables)
1062
1091
0 commit comments