On this page
inspect.getclosurevars
Prev: inspect.getclasstree | Up: inspect | Next: inspect.getcomments
Rel: inspect.ArgInfo, inspect.Arguments, inspect.Attribute, inspect.BlockFinder
Ref: docs.python.org — inspect.getclosurevars
Tags: #public #python3 #stdlib #inspect
Import
import inspect
from inspect import getclosurevars
Signature & Overview
inspect.getclosurevars(func)
Get the mapping of free variables to their current values.
Usage
import inspect
def outer(factor: int):
return lambda n: n * factor
cv = inspect.getclosurevars(outer(3))
assert cv.nonlocals == {"factor": 3}
Gotchas
- Great for debugging late-binding closure bugs in loops: Inspect
cv.nonlocalsto see the exact values currently bound in cell variables.
REPL Inspect
from inspect import getclosurevars
help(getclosurevars)
Linked from
Related notes
- tuts/python3/stdlib_module/inspect/getclasstree Linked here; links to this page; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/getcomments Linked here; links to this page; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/inspect/ArgInfo Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/Arguments Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/Attribute Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/BlockFinder Linked here; shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/BoundArguments shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/BufferFlags shared tags: #inspect, #stdlib
- tuts/python3/stdlib_module/inspect/ClassFoundException shared tags: #inspect, #stdlib