python logo

what is scope in python


Python hosting: Host, run, and code Python in the cloud!

Understanding Python Scope
In Python, the scope of a variable determines the portion of the program where you can access that specific variable. Not every variable can be accessed from any location in a program.

Local vs Global Variables in Python
Python supports two types of variables based on their scope:

  • Global Variables: These are declared outside a function and can be accessed throughout the program.
  • Local Variables: These are declared inside a function and can be accessed only within that function.

If you’re creating a variable inside a function, it’s local by default. However, if you need to access or modify a global variable from within a function, you must use the global keyword before the variable.

Python Scope Example:
Let’s delve into examples to understand the use of local and global variables better. Here’s an example where a local variable inside a function is not accessible, and hence the code will not work:

1
2
3
4
5
6
7
def f(x,y):
print('You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y))
print('x * y = ' + str(x*y))
z = 4 # z is local here, and is not accessible outside the function

z = 3
f(3,2)

In contrast, here’s how defining the variable within the function scope allows it to be used:

1
2
3
4
5
6
7
def f(x,y):
z = 3
print('You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y))
print('x * y = ' + str(x*y))
print(z) # z is local and defined within the function

f(3,2)

Another example where all the required variables are passed as parameters to the function:

1
2
3
4
5
def f(x,y,z):
return x+y+z # returns the sum as all variables are provided as arguments

result = f(3,2,1)
print(result)

Nested Functions and Variable Scope
It’s possible to call functions within functions. When doing so, you can also fetch the value of a variable from another function. Let’s explore this with an example:

1
2
3
4
5
6
7
8
9
def highFive():
return 5

def f(x,y):
z = highFive() # fetching value from the highFive function
return x+y+z

result = f(3,2)
print(result)

To summarize, if a variable is accessible throughout the entire code, it’s termed as a global variable. If its access is restricted to a particular portion (like within a function), it’s termed a local variable.

Ready for more practice? Download Python Exercises

⬅️ Back | ➡️ Next






Leave a Reply:




Carl Wainwright Sun, 28 Aug 2016

I ran this code in v2.7.10 and it executed without any errors


def f(x,y):
print('You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str(y))
print('x * y = ' + str(x*y))
z = 4 # cannot reach z, so THIS WON'T WORK
print(z)
z = 3
f(3,2)


MacBookAir:.ssh carlwainwright$ python ~/Projects/Python/helloworld.py
You called f(x,y) with the value x = 3 and y = 2
x * y = 6
4

Frank Sun, 28 Aug 2016

Try this program:


def showz():
print(z)
def f(x,y):
print('You called f(x,y) with the value x = ' + str(x) + ' and y = ' + str($
print('x * y = ' + str(x*y))
z = 4 # cannot reach z, so THIS WON'T WORK
print(z)
z = 3
f(3,2)
showz()

In this example z will still be 3 after calling f(3,2). Sorry for the confusion, ill update

Carl Wainwright Mon, 29 Aug 2016

So without "global z" in function 'f' z is local to the function and when you return back the global z is scoped.

Frank Wed, 31 Aug 2016

Correct, otherwise the changed occur only locally

Greg Sun, 23 Jul 2017

To follow up on Carl's comment, the supplied example in the tutorial (with the bold, italic "this will not work") also works fine for me in Python 3.6. I thought I'd let everyone know what I discovered while experimenting to find what would and would not work.

Without using the keyword 'global'...

You can reference a global variable inside a function. This code works fine, and prints the global value of z (10) twice:


def testing_scope():
print(z)

z = 10
testing_scope()
print(z)


You can also create a local variable with the same name inside a function. This code works fine, and prints the local value of z (1) followed by the global value of z (10):

def testing_scope():
z = 1
print(z)

z = 10
testing_scope()
print(z)


However, you cannot first reference global variable then create a local variable with the same name. Trying to define testing_scope like this does not work:

def testing_scope():
print(z)
z = 1

I hope that helps clarify things!

Greg Sun, 23 Jul 2017

My apologies for the indentation not showing up properly. I would edit if I could.

Frank Wed, 26 Jul 2017

No problem, all indented correctly now :)