First thing's first. Changes to assignments should not be emailed by the profs after one has finished the assignment. Just...no. Needless to say that all the time spent in lecture on discussion TOAH model and assignment 1 in general would've been better spent last week (personally/for my group), but it was a good confirmation that our group hadn't gone in the completely wrong direction, but I digress; at least there will be less to write about in this week's slog.
What new material we did cover in lecture revolved around how Python handles scopes and namespaces.
But before we begin, what is a name? Remember from CSC108 that Python names refer to a memory address which contains an object, being a value, function, class, etc. A namespace is ...well a space that holds several names.
Consider a Python module/file (insert_name_here.py). Each module has its own global namespace, so that all the names used in the module are in the same space. Because of this, no two objects can have the same name, unless they are nested (but more on that later). Even though each individual module has its own global namespace where no two objects can have the same name, you can import different modules into your program. When you do this, there can be a name shared by both modules that refer to different objects. For example if I have the file stack.py with the class method is_empty(), and queue.py with the class method is_empty(), they can both be called separately by importing both modules (import stack and import queue) and then typing stack.is_empty() and queue.is_empty(). However note that if the program has an is_empty() function, access to the functions of the same name from stack and queue will be lost.
Scopes are regions of a program from where a namespace can be accessed without a prefix (like the above where the prefix stack. referred to the module name). At any time, there are several different scopes being used in a program - the scope of the current function, module, and the Python built-ins. If the functions/modules are un-nested, they cannot access names from other functions/modules in the program. For a simple example consider the following:
Even though the functions scream and shush have the same variable names s and new_s, the two functions still work when they are called in the shell without interfering with each other.
However, what if the functions were nested? Consider the following example from class:
First, Python acknowledges that the functions do_local, do_nonlocal, and do_global exist before executing spam = 'test spam'. The function do_local creates a spam that is local, meaning it only exists in the scope of the function do_local and not in any other function, even scope_test. When the do_local is called later within scope_test, the value for spam is automatically trashed after the function finishes executing. In do_nonlocal, the operation nonlocal makes it so that the spam that do_nonlocal uses is the spam from the encompassing function scope_test, which states that spam = 'test spam'. However the next line of the function dictates that spam = 'nonlocal spam' which changes the spam in the encompassing scope as well. When do_global is called, since at this point, there is no name spam in the global namespace, it creates a global variable spam, and then assigns its value as the string 'global spam'. However, because do_global() is being called within scope test, and not the global scope, it prints the value of spam that is within the local scope of scope_test, which at this point, from the previous assignment of do_nonlocal is the string 'nonlocal spam'. When the function finally exits scope_test, and prints spam, it first searches the local namespace, and fines no name spam in that namespace so it then checks the global namespace, to find that in the global namespace, spam = 'global spam', and hence prints that.
So in summary, python always looks for names in different scopes in the following order:
- The innermost scope - local variables
- Encompassing scopes - nonlocal variables
- The global scope - global variables
- Built-ins
Namespaces are, in my opinion, can get quite confusing, but since we haven't encountered any really complicated code that involves switching between many layer of nonlocal scopes, it hasn't been too difficult to use.
...But I'm sure we'll be dealing with really confusing scenarios on the term test. Which is in less than two weeks.


No comments:
Post a Comment