Online Book Reader

Home Category

Learning Python - Mark Lutz [243]

By Root 1315 0
to change a mutable object in the enclosing scope in 2.X and 3.X without declaring its name nonlocal (e.g, state=[start] in tester, state[0]+=1 in nested), though it's perhaps more obscure than either function attributes or 3.X's nonlocal.

Chapter Summary

In this chapter, we studied one of two key concepts related to functions: scopes (how variables are looked up when they are used). As we learned, variables are considered local to the function definitions in which they are assigned, unless they are specifically declared to be global or nonlocal. We also studied some more advanced scope concepts here, including nested function scopes and function attributes. Finally, we looked at some general design ideas, such as the need to avoid globals and cross-file changes.

In the next chapter, we’re going to continue our function tour with the second key function-related concept: argument passing. As we’ll find, arguments are passed into a function by assignment, but Python also provides tools that allow functions to be flexible in how items are passed. Before we move on, let’s take this chapter’s quiz to review the scope concepts we’ve covered here.

Test Your Knowledge: Quiz

What is the output of the following code, and why?>>> X = 'Spam'

>>> def func():

... print(X)

...

>>> func()

What is the output of this code, and why?>>> X = 'Spam'

>>> def func():

... X = 'NI!'

...

>>> func()

>>> print(X)

What does this code print, and why?>>> X = 'Spam'

>>> def func():

... X = 'NI'

... print(X)

...

>>> func()

>>> print(X)

What output does this code produce? Why?>>> X = 'Spam'

>>> def func():

... global X

... X = 'NI'

...

>>> func()

>>> print(X)

What about this code—what’s the output, and why?>>> X = 'Spam'

>>> def func():

... X = 'NI'

... def nested():

... print(X)

... nested()

...

>>> func()

>>> X

How about this example: what is its output in Python 3.0, and why?>>> def func():

... X = 'NI'

... def nested():

... nonlocal X

... X = 'Spam'

... nested()

... print(X)

...

>>> func()

Name three or more ways to retain state information in a Python function.

Test Your Knowledge: Answers

The output here is 'Spam', because the function references a global variable in the enclosing module (because it is not assigned in the function, it is considered global).

The output here is 'Spam' again because assigning the variable inside the function makes it a local and effectively hides the global of the same name. The print statement finds the variable unchanged in the global (module) scope.

It prints 'NI' on one line and 'Spam' on another, because the reference to the variable within the function finds the assigned local and the reference in the print statement finds the global.

This time it just prints 'NI' because the global declaration forces the variable assigned inside the function to refer to the variable in the enclosing global scope.

The output in this case is again 'NI' on one line and 'Spam' on another, because the print statement in the nested function finds the name in the enclosing function’s local scope, and the print at the end finds the variable in the global scope.

This example prints 'Spam', because the nonlocal statement (available in Python 3.0 but not 2.6) means that the assignment to X inside the nested function changes X in the enclosing function’s local scope. Without this statement, this assignment would classify X as local to the nested function, making it a different variable; the code would then print 'NI' instead.

Although the values of local variables go away when a function returns, you can make a Python function retain state information by using shared global variables, enclosing function scope references within nested functions, or using default argument values. Function attributes can sometimes allow state to be attached to the function itself, instead of looked up in scopes. Another alternative, using OOP with classes, sometimes supports state retention better than any of the scope-based techniques because it makes it explicit

Return Main Page Previous Page Next Page

®Online Book Reader