Dec-27-2019, 09:08 AM
This is topic with lot of subtlety. Some of the scenarios are discussed in this thread: Namespace and scope difference
I think that this boils down to this:
I think that this boils down to this:
Quote:You should also understand difference between reference and assignment:
When you reference a variable in an expression, the Python interpreter will traverse the scope to resolve the reference in following order:
- current function’s scope
- any enclosing scopes (like containing functions)
- scope of the module that contains the code (global scope)
- built-in scope (that contains functions like int and abs)
If Python doesn't find defined variable with the referenced name, then a NameError exception is raised.
Assigning a value to a variable works differently. If the variable is already defined in the current scope, then it will just take on the new value. If the variable doesn’t exist in the current scope, then Python treats the assignment as a variable definition. The scope of the newly defined variable is the function that contains the assignment.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.