Python: Parameter Scope

This is part of my Python & Django Series which can be found here including information on how to download all the source code.

Variables can be local or global.

Global Variables exist at the top level; i.e they are not inside a class or a method. They are implicitly global.

Local Variables are any variables inside a method. They will shadow a variable with the same name in an outer scope, i.e the calling method or class class etc. Changes to a local variable does not affect any variables which they shadow.

Global keyword is used to import a global variable. It allows a global variable to be used and set inside a method. It can only import global variables and not outer scope variables such as those within an outer method or class.

Nonlocal keyword is similar to global but will import any variables which are in an outer scope regardless if they are global or not global.

The following code and output can be used to demonstrate the usage of the above concepts.

Code:

def print_scope_msg(a_msg):
    print("Inside scope_test:", a_msg)

msg = "Initial"

def scope_test():

    def local_test():
        # msg is local. Changes are only existing inside the method
        msg = "local"
        print("Inside local_test():", msg)

    def nonlocal_test():
        # msg is non local. This allows the msg inside scope_test() to be imported and edited
        nonlocal msg
        msg = "nonlocal"
        print("Inside nonlocal_test:", msg)

    def global_test():
        # msg is global. This allows the msg inside global but not scope_test() to be imported and edited
        # Changes to msg affect the global msg but not the scope_test (it skips it)
        global msg
        msg = "global"
        print("Inside global_test: ", msg)

    msg = "scope"

    local_test()
    print_scope_msg(msg)

    nonlocal_test()
    print_scope_msg(msg)

    global_test()
    print_scope_msg(msg)

scope_test()
print("Inside global:", msg)

Output:

1: Inside local_test(): local
2: Inside scope_test: scope
3: Inside nonlocal_test: nonlocal
4: Inside scope_test: nonlocal
5: Inside global_test: global
6: Inside scope_test: nonlocal
7: Inside global: global

So what happened? Each numbered output above has an explanation as described below.

  1. Reports “local” as we have changed msg to local.
  2. Reports “scope” as the change inside local_test() was made to a shadowed version of msg which does not affect the value of msg inside scope_test().
  3. Reports “nonlocal” as we have changed msg to nonlocal.
  4. Reports “nonlocal” as inside nonlocal_test() we imported msg with the nonlocal keyword before changing the value.
  5. Reports “globaltest” as we have changed msg to globaltest.
  6. Reports “nonlocal” as the change to msg in global_test() used the global keyword which imported the global reference of msg and not that defined within scope_test() where we are now.
  7. Reports “globaltest” as the change to msg in global_test() used the global keyword.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s