4.13 Scope Rules

Local Scope

  • A local variable’s identifier has local scope.

Global Scope

  • Identifiers defined outside any function (or class) have global scope—these may include functions, variables and classes.

Accessing a Global Variable from a Function

In [1]:
x = 7
In [2]:
def access_global():
    print('x printed from access_global:', x)
In [3]:
access_global()
x printed from access_global: 7
  • By default, you cannot modify a global variable in a function
  • Python creates a new local variable when you first assign a value to a variable in a function’s block.
  • In function try_to_modify_global’s block, the local x shadows the global x, making it inaccessible in the scope of the function’s block.
In [4]:
def try_to_modify_global():
    x = 3.5
    print('x printed from try_to_modify_global:', x)
In [5]:
try_to_modify_global()
x printed from try_to_modify_global: 3.5
In [6]:
x
Out[6]:
7
  • To modify a global variable in a function’s block, you must use a global statement to declare that the variable is defined in the global scope:
In [7]:
def modify_global():
    global x;
    x = 'hello'
    print('x printed from modify_global:', x)
    
In [8]:
modify_global()
x printed from modify_global: hello
In [9]:
x
Out[9]:
'hello'

Blocks vs. Suites

  • When you create a variable in a block, it’s local to that block.
  • When you create a variable in a control statement’s suite, the variable’s scope depends on where the control statement is defined:
    • If it's in the global scope, any variables defined in the control statement have global scope.
    • If it's in a function’s block, any variables defined in the control statement have local scope.

Shadowing Functions

  • In the preceding chapters, when summing values, we stored the sum in a variable named total.
  • If you define a variable named sum, it shadows the built-in function sum, making it inaccessible in your code.
In [10]:
sum = 10 + 5
In [11]:
sum
Out[11]:
15
In [12]:
sum([10, 5])
------------------------------------------------------------------------
TypeError                              Traceback (most recent call last)
<ipython-input-12-1237d97a65fb> in <module>
----> 1 sum([10, 5])

TypeError: 'int' object is not callable

Statements at Global Scope

  • Script statements at global scope execute as soon as they’re encountered by the interpreter, whereas statements in a block execute only when the function is called.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 4 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.