What exactly are the Python scoping rules?
If I have some code:
code1 class Foo: code2 def spam..... code3 for code4..: code5 x()
x found? Some possible choices include the list below:
- In the enclosing source file
- In the class namespace
- In the function definition
- In the for loop index variable
- Inside the for loop
Also there is the context during execution, when the function
spam is passed somewhere else. And maybe lambda functions pass a bit differently?
There must be a simple reference or algorithm somewhere. It's a confusing world for intermediate Python programmers.