Python Lexical Scoping

PHOTO EMBED

Mon Jan 30 2023 18:04:15 GMT+0000 (Coordinated Universal Time)

Saved by @abell_ds

def get_adder(summand1: float) -> Callable[[float], float]:
    """Returns a function that adds numbers to a given number."""
    def adder(summand2: float) -> float:
        return summand1 + summand2

    return adder
content_copyCOPY

A nested Python function can refer to variables defined in enclosing functions, but cannot assign to them. Variable bindings are resolved using lexical scoping, that is, based on the static program text. Any assignment to a name in a block will cause Python to treat all references to that name as a local variable, even if the use precedes the assignment. If a global declaration occurs, the name is treated as a global variable.

https://google.github.io/styleguide/pyguide.html