Sunday, 9 February 2014

how python searches for names

    Last week, I learned how python searches for names. Python programs call variable names all the time. Things can get confusing when the same name is used in different places of the same code to define different things. This is why python searches for names in a particular order:

-First, it searches for a local variable name (a variable usually defined within the same function as the call)
-Second, if there is no local variable name, it searches for a nonlocal variable (a variable defined not within the same function as the call, but instead defined in a larger function that includes the function with the call)
-Third, if there is no nonlocal variable name, it searches for a global variable (found on module level or under __main__)
-Finally, if there is no global variable name, it looks for any built-in python names

    I prepared an example of python code where this order comes into play:

vari = "spam0"

def my_scope_test():
    vari = "spam1"
    def inner_function1():
        global vari
        vari = "spam2"
        print("inner function1() searches for:", vari)
    
    inner_function1()
    def inner_function2():
        print("inner_function2() searches for:", vari)
        
    inner_function2()
    print("my_scope_test() searches for:  ", vari)

my_scope_test()
print("the module level searches for: ", vari)


When this code runs: here is what is printed:

inner function1() searches for: spam2
inner_function2() searches for: spam1
my_scope_test() searches for:   spam1
the module level searches for:  spam2

The variable named "vari" is defined three times: First at the module level, second within a function, and third within an inner function, but with a global assignment. (That makes the definition as if it were made in the module level, and how it would be defined without the global assignment) In the same order, these definitions at different points in the code are considered global, local/nonlocal, and global/local. The variations depend on which stage in the code execution these variables are called. I will now walk you through python's thought process for each name search:

inner_function1() needs to find vari to use for it's print statement.
Step 1: local variable name? Yes!
So it uses the variable definition within inner_function1() for the print statement.

inner_function2() needs to find vari to use for it's print statement.
Step 1: local variable name? No
Step 2: nonlocal variable name? Yes!
So it uses the variable definition within the overlapping function my_scope_test() for the print statement.

my_scope_test() needs to find vari to use for it's print statement.
Step 1: local variable name? Yes!
So it uses the variable definition within my_scope_test() for the print statement.

the module level needs to find vari to use for it's print statement.
Step 1: local variable name? Yes!
The module level's local variable names are the global variable names. The global definition for vari has been written twice, so it uses the latest definition, which occured when inner_function1() ran.

    Later in this same lecture when I learned all of this, it was demonstrated that this kind of logic also occurs when working with classes and methods, instead of what we just did with functions. Python looks for methods in the class before looking for methods in the superclass. This order of searching for methods in class and superclasses is called inhertance, which is very similar to the all the name searching I just wrote about.
    That's everything I know about name searching, I might make another post about Assignment 1 after it's finished and after the due date has passed.

No comments:

Post a Comment