In order to fully understand namespace and scope you should familiarise yourself with closure as well. Python supports closures: functions that refer to variables from the scope in which they were defined.
ichabod801 used terms 'typically' and 'without some tricks' for a reason. There is much subtlety and in order to understand you must look at the whole picture.
There are behaviours which seem illogical at first but actually make sense if you think about it. Just to illustrate:
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.
ichabod801 used terms 'typically' and 'without some tricks' for a reason. There is much subtlety and in order to understand you must look at the whole picture.
There are behaviours which seem illogical at first but actually make sense if you think about it. Just to illustrate:
>>> def inner(): ... y = 1 ... def innermost(): ... y = 2 ... innermost() ... print(y) ... >>> inner() 1 >>> def inner(): ... y = [1] ... def innermost(): ... y[0] = 2 ... innermost() ... print(y) ... >>> inner() [2]EDIT:
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.