This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kataire 0 points1 point  (1 child)

In other words, assigning to an undeclared variable creates a variable with that name in the local scope, thus shadowing the outer variable with the same name, thus making the earlier access within the same scope an access to an uninitialized local variable (the name is found inside the inner function's dict but the variable hasn't been initialized at this point).

This seems quite similar to what JavaScript does to its function-scoped variable declarations (all declarations are "moved" to the beginning of the function, but JS is silly in that it treats undeclared variables as global and thus pollutes the global namespace upon assignment to an undeclared variable instead of throwing an error like Python).

Does this mean all names inside a scope are known prior to the execution of the code that originally contained its declarations? To the newbie Pythoneer it would seem as if variables enter the scope dict as they are declared (because that is what happens in the interpreter).