Skip to content

Commit e4bf977

Browse files
Clarify the LOAD_FAST docs.
1 parent 4d82f15 commit e4bf977

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Doc/library/dis.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,11 +1043,20 @@ All of the following opcodes use their arguments.
10431043

10441044
.. opcode:: LOAD_FAST (var_num)
10451045

1046-
Pushes a reference to the local ``co_varnames[var_num]`` onto the stack.
1047-
Closures are also handled by this operation.
1046+
Pushes a reference to a local variable or closure cell onto the stack.
1047+
The corresponding variable name is
1048+
``(co_varnames + co_cellvars + co_freevars)[var_num]``.
1049+
1050+
For closures, note that ``LOAD_FAST`` loads the cell object contained
1051+
in the corresponding slot of the cell and free variable storage,
1052+
at index ``var_num - len(co_varnames)``.
1053+
In contrast, ``LOAD_DEREF`` gets the object the cell references.
1054+
1055+
Use of ``LOAD_FAST`` for closures is primarily to share cells objects
1056+
from an outer closure when creating an inner one with ``MAKE_FUNCTION``.
10481057

10491058
.. versionchanged:: 3.10
1050-
Closures are handled here now instead of ``LOAD_CLOSURE`` (removed).
1059+
Closure cells are handled here now instead of ``LOAD_CLOSURE`` (removed).
10511060

10521061

10531062
.. opcode:: STORE_FAST (var_num)

0 commit comments

Comments
 (0)