- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the basic scoping rules for python variables?
Variables are classified into Global variables and Local variables based on their scope. The main difference between Global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined.
Local variables are those that are defined inside a function but have a scope that is only applicable to that function, as opposed to global variables, which are defined outside of any function and have a global scope. In other words, we may argue that although global variables are accessible across the program and inside all functions, local variables are only accessible within the function in which they were initialized. Local variables are variables that are created within a function and are exclusive to that function. It can't be accessible outside of the function anyplace. Let's examine the creation of a local variable.
Local Variables in Python
A local variable is a specific kind of variable that can be utilized when the method or statement block in which it is declared defines the variable's scope and extend. The for each statement uses it as an iteration variable, the specific-catch clause uses it as an exception variable, and the using statement uses it as a resource variable.
In the method or statement block where it is declared, it can also be used as a constant whose value cannot be changed. Language integrated queries (LINQ), which return anonymous types, can be dealt with by using an implicitly typed local variable whose type is deduced by the compiler from the expression to its right. This allows a custom type to be created for each LINQ result set.
Let’s look at an example to create a local variable.
def f(): #local variable L = "Hello World" print(L) # Driver code f()
The output is as follows.
Only when assigning values to or changing global variables within a function do we need to use the global keyword. For printing and accessing, global is not necessary. Due to the assignment to s inside of f(), Python "assumes" that we want a local variable, which is why the first sentence throws the error. If a variable is modified or created inside of a function without being defined as a global variable, it is considered local.
The following is an example of making a global variable and accessing it.
pi = 3.142 radius = 8 def circle(): #radius is a global varible global radius radius = radius * 2 area_of_circle = pi * (radius) ** 2 print("The area of circle is: ", area_of_circle) circle()
The output produced is as follows.
The area of circle is: 804.352
Enclosing or Nonlocal Scope
The variable that is specified in the nested function is known as Nonlocal Variable. It indicates that the variable cannot be both local and global in scope. The nonlocal keyword is used to generate nonlocal variables. The inner function is nested inside the outer function that we generated in the following code (). Inner() function is defined in the outer() function's scope. Changes made to the nonlocal variable declared in the inner() function are mirrored in the output of the outer function.
The following example demonstrates the usage
def Outer(): x = "local" def Inner(): nonlocal x x = "nonlocal" print("inner:", x) Inner() print("outer:", x) Outer()
The output is as follows.
inner: nonlocal outer: nonlocal
- What are the basic rules for defining variables in C++?
- What are the scoping rules for lambda expressions in Java?\n
- What are the rules for local and global variables in Python?
- What are the basic rules and idioms for operator overloading in C++?
- What are the rules to declare variables in C++?
- What are the identity rules for regular expression?
- What are the golden rules for handling your money?
- What are the rules for naming classes in C#?
- What are the basic concepts of Python?
- What are the rules for calling the superclass constructor C++?
- What are the rules for a functional interface in Java?
- What are the rules for the Subscriber interface in Java 9?
- What are the rules for the Subscription interface in Java 9?
- What are the rules for the Publisher interface in Java 9?