Book Image

Python Essentials

By : Steven F. Lott
Book Image

Python Essentials

By: Steven F. Lott

Overview of this book

Table of Contents (22 chapters)
Python Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with namespaces


When a function is evaluated, Python creates a local namespace. The parameter variables are created in this local namespace when the argument values (or default values) are assigned. Any variables that are created in the suite of statements in the function's body are also created in this local namespace.

As we noted in Chapter 4, Variables, Assignment and Scoping Rules, each object has a reference counter. An object provided as an argument to a function will have the reference count incremented during the execution of the function's suite of statements.

When the function finishes—either because of an explicit return statement or the implicit return at the end of the suite—the namespace is removed. This will decrement the number of references to the argument objects.

When we evaluate an expression like more_dice_good(2, hand), the literal integer 2 will be assigned to the n parameter variable. Its reference count will be one during the execution of the function. The object...