1
0
mirror of https://github.com/satwikkansal/wtfpython synced 2024-11-22 11:04:25 +01:00
getclosurevals -> getclosurevars
This commit is contained in:
abdo 2021-02-03 02:01:07 +03:00 committed by GitHub
parent 048a620502
commit de4b851c72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

2
README.md vendored
View File

@ -1027,7 +1027,7 @@ The values of `x` were different in every iteration prior to appending `some_fun
* When defining a function inside a loop that uses the loop variable in its body, the loop function's closure is bound to the *variable*, not its *value*. The function looks up `x` in the surrounding context, rather than using the value of `x` at the time the function is created. So all of the functions use the latest value assigned to the variable for computation. We can see that it's using the `x` from the surrounding context (i.e. *not* a local variable) with: * When defining a function inside a loop that uses the loop variable in its body, the loop function's closure is bound to the *variable*, not its *value*. The function looks up `x` in the surrounding context, rather than using the value of `x` at the time the function is created. So all of the functions use the latest value assigned to the variable for computation. We can see that it's using the `x` from the surrounding context (i.e. *not* a local variable) with:
```py ```py
>>> import inspect >>> import inspect
>>> inspect.getclosurevals(funcs[0]) >>> inspect.getclosurevars(funcs[0])
ClosureVars(nonlocals={}, globals={'x': 6}, builtins={}, unbound=set()) ClosureVars(nonlocals={}, globals={'x': 6}, builtins={}, unbound=set())
``` ```
Since `x` is a global value, we can change the value that the `funcs` will lookup and return by updating `x`: Since `x` is a global value, we can change the value that the `funcs` will lookup and return by updating `x`: