python variable scope
The scope of a variable alludes to where it can be accessed in a program. A variable's scope depends on where it is announced and is confined to the piece in which it was defined. Program engineers understand the contrast between local variables, limited to a particular block, and global variables, which can be accessed throughout the complete program.
In Python programming, distinctive scopes of variables can be utilized to form effective programs. The scope of a variable refers to where it can be accessed and used inside a program. The scope decides which parts of the code can utilize the variable and its corresponding value. There are two fundamental types of scopes for variables:
Local variables are the variables that are declared inside a function. It can only be accessed within that function and is not visible to the rest of the program. Once the function has finished executing, the local variable is destroyed. Here's an example:
def my_functiox = 10 pr (x) my_functio () # Output: 10 pr (x) # Output: NameError: name 'x' is not defined():
In this example, we declare a local variable x inside the function my_function(). We can access this variable within the function, but outside of the function, it is not defined.
Global variables 📈 are the variables that are declared outside of any function. It can be accessed anywhere in the program, including within functions. Here's an example:
x = 10 def my_functio (): pr (x) my_functio () # Output: 10 pr (x) # Output: 10
In this example, we declare a global variable x 📈 outside the function my_function(). We can access this variable from inside the function as well as outside of it.
Suppose you want to modify the value of a global variable inside a function. In that case, you need to use the global keyword to tell Python that you are referring to the global variable, not a local variable with the same name. Here's an example:
x = 10 def my_function(): global x x = 20 print(x) my_function() # Output: 20 print(x) # Output: 20
In this illustration, we announce a global variable x outside the function my_function(). Inside the function, we utilize the global keyword to indicate that we refer to the global variable x, not a local variable. We then adjust the value of x to 20 and print it out. After the function has wrapped up executing, the value of the global variable x has been upgraded to 20.
Nonlocal may be a keyword in Python utilized to declare a variable as nonlocal (outside the current scope). This implies that the variable can be referenced and adjusted inside a nested function. The nonlocal keyword declares a variable as nonlocal if defined outside the current scope.
def outer_func(): x = "local" def inner_func(): nonlocal x x = "nonlocal" print("inner:", x) inner_func() print("outer:", x) outer_func()
This code uses the nonlocal keyword to declare a variable as nonlocal (outside the current scope). The variable x is declared local in the outer_func() function and then modified as nonlocal in the inner_func() function. When inner_func() is called, the variable x is modified to "nonlocal", and then the outer_func() prints the value of x as "nonlocal".
Apart from local and global variables, there are two more scopes. Let's explore them.
LEGB stands for Local, Enclosing, Global, and Built-in. The rule determines the order in which Python looks for a variable when referenced.
The LEGB rule states that Python looks for a variable in the following order:
Python has two main types of variable scopes: local and global🌏. Local variables are limited to the block in which they are declared, while global variables can be accessed throughout the program📑. To modify the value of a global variable inside a function, the global keyword is used. The nonlocal keyword is used to declare a nonlocal variable even if the variable is defined outside the current scope. Python uses the LEGB rule to determine the order in which it looks for a variable: Local, Enclosing, Global, and Built-in.
Answer:b. Global variables can be accessed locally
Answer:a. Local, Enclosing, Global, Built-in
Answer: a. The local variable is destroyed.
Related Tutorials to watch
Top Articles toRead