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 | def f(x,y): |
In contrast, here’s how defining the variable within the function scope allows it to be used:
1 | def f(x,y): |
Another example where all the required variables are passed as parameters to the function:
1 | def f(x,y,z): |
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 | def highFive(): |
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
Leave a Reply:
I ran this code in v2.7.10 and it executed without any errors
Try this program:
In this example z will still be 3 after calling f(3,2). Sorry for the confusion, ill update
So without "global z" in function 'f' z is local to the function and when you return back the global z is scoped.
Correct, otherwise the changed occur only locally
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:
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):
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:
I hope that helps clarify things!
My apologies for the indentation not showing up properly. I would edit if I could.
No problem, all indented correctly now :)