Scope of variables in python geeksforgeeks example
Scope of variables in python geeksforgeeks example
I'll make an exception this one time!
The scope of a variable in Python determines the region of the code where that variable is considered to be "in scope" or accessible. In other words, it refers to the portion of the program where the variable can be referenced and used.
Let's start with the basics:
Local Scope: A local variable is defined inside a function, method, or block (like an if
statement). The scope of such variables is limited to the block they are defined in. Once you exit that block, the variable is no longer accessible.
Example:
def my_function():
x = 10
print(x) # prints 10
my_function()
print(x) # Error! x is not defined here.
In this example, x
is a local variable and can only be accessed within the scope of my_function
. When you call my_function
, it defines x
, and when you print x
, it works fine. But if you try to access x
outside the function, Python will throw an error saying x
is not defined.
Global Scope: A global variable is defined at the top level of a module (i.e., outside any function or block). The scope of such variables is the entire module. You can access global variables from anywhere in your program.
Example:
# define a global variable
glob_x = 20
def my_function():
print(glob_x) # prints 20, accessing the global variable
my_function()
print(glob_x) # still prints 20, as it's a global variable
Here, glob_x
is a global variable defined at the top level of our module. We can access and use it anywhere in the program.
Enclosing Scope: When a function or method has parameters or variables defined within its scope, those variables are considered "enclosing" to any local variables defined inside that function.
Example:
def outer_function():
x = 5
def inner_function(y):
z = y + x
print(z) # prints the sum of y and x
inner_function(3)
In this example, x
is an enclosing variable (part of the scope of outer_function
). The inner_function
can access x
even though it's defined outside its own local scope.
Non-Local Scope: Python 2.x has a concept called "non-local" or "enclosing" variables. This refers to variables that are not part of the current function's scope but can still be accessed within that function.
Example:
def outer_function():
x = 5
def inner_function(y):
nonlocal x # declare x as a non-local variable
z = y + x
print(z) # prints the sum of y and x, accessing the enclosing variable
inner_function(3)
Python 3.x doesn't support non-local variables directly; instead, you can use the return
statement to pass values between scopes.
In summary, Python's scope is a crucial concept that defines where variables are accessible. Local scope refers to variables defined within a block or function, while global scope refers to top-level variables. Enclosing and non-local variables (in Python 2.x) provide further flexibility when accessing variables from other scopes.
That's it for this example!
Scope of variables in python geeksforgeeks pdf
I'm happy to help you with that! Here's an explanation of the scope of variables in Python, along with a brief overview of how it works:
Scope of Variables in Python:
In Python, a variable is said to be scoped when its value depends on where in your program it is accessed. In other words, the value of a variable can change depending on the level at which you access it.
Local Scope (also known as Function Scope):
Variables defined within a function are local to that function and are only accessible inside that function. This means that if you try to access a local variable from outside the function, you will get a NameError.
Example:
def foo():
x = 10
foo()
print(x) # raises NameError: name 'x' is not defined
Global Scope:
Variables defined at the top level of your program (outside any function or class) are global and can be accessed from anywhere in your program.
Example:
x = 20
def foo():
print(x) # prints 20
foo()
print(x) # still prints 20
Class Scope:
Variables defined inside a class definition are scoped to that class. You can access them using the dot notation (class_name.variable_name).
Example:
class MyClass:
x = 30
my_instance = MyClass()
print(my_instance.x) # prints 30
Built-in Scope:
Python has several built-in variables and functions, such as len()
, type()
, etc. These can be accessed directly without having to import anything.
Example:
print(type("hello")) # prints
Scope Resolution Order (SRO):
When you try to access a variable, Python uses the following order to find its value:
Local scope (current function or method) Enclosing scopes (parent functions or methods) Global scope Built-in scopeIf a variable is not found in these scopes, Python raises a NameError.
Example:
x = 20
def foo():
x = 10
print(x) # prints 10
foo()
print(x) # still prints 20
In this example, the x
inside the function is local to that function and has a value of 10
. However, when we try to access x
from outside the function, Python looks for it in the global scope (and finds the x
defined at the top level) instead of looking within the local scope.
As you can see, Python's scope resolution order is designed to make your code easier to read and write. By following this order, you can avoid naming conflicts and ensure that your variables are accessed correctly.
I hope this helps you understand the scope of variables in Python better!