Local And Global Variables
Variable Scope in Python
Section titled “Variable Scope in Python”In general, a variable that is defined in a block is available in that block only. It is not accessible outside the block. Such a variable is called a local variable. Formal argument identifiers also behave as local variables.
variable
The following example will underline this point. An attempt to print a local variable outside its scope will raise the NameError exception.
NameErrorexception
def greet(): name = 'Steve' print('Hello ', name)
greet()print(name) #NameError`def greet(): name = ‘Steve’ print(‘Hello ’, name)
greet()
print(name) #NameError[Try it](/codeeditor?cid=python-3z7yc8y8j) Here, name is a local variable for the greet() function and is not accessible outside of it. name“greet() Any variable present outside any function block is called a global variable. Its value is accessible from inside any function. In the following example, the name variable is initialized before the function definition. Hence, it is a global variable. [function](/python/python-user-defined-function)name`
name='John'def greet(): print ("Hello ", name)
greet()print(name)`name=‘John’ def greet(): print (“Hello ”, name)
greet()
print(name)[Try it](/codeeditor?cid=python-3z7ycbp95) Here, you can access the global variable name because it has been defined out of a function. name`
However, if we assign another value to a globally declared variable inside the function, a new local variable is created in the function’s namespace. This assignment will not alter the value of the global variable. For example:
name = 'Steve'def greet(): name = 'Bill' print('Hello ', name) #Hello Bill
greet()print(name) #steve`name = ‘Steve’ def greet(): name = ‘Bill’ print(‘Hello ’, name) #Hello Bill
greet()
print(name) #steve[Try it](/codeeditor?cid=python-3z7ycff7a) Now, changing the value of global variable name inside a function will not affect its global value. nameIf you need to access and change the value of the global variable from within a function, this permission is granted by the global keyword.global`
name = 'Steve'def greet(): global name name = 'Bill' print('Hello ', name)
greet()print(name) #Bill`name = ‘Steve’ def greet(): global name name = ‘Bill’ print(‘Hello ’, name)
greet()
print(name) #Bill[Try it](/codeeditor?cid=python-3z7yckd6s) The above would display the following output in the Python shell. [Python shell](/python/python-interective-shell) It is also possible to use a global and local variable with the same name simultaneously. Built-in function globals() returns a dictionary object of all global variables and their respective values. Using the name of the variable as a key, its value can be accessed and modified. globals()`
name = 'Steve'def greet(): globals()['name'] = 'Ram' name='Steve' print ('Hello ', name)
greet()print(name) #Ram`name = ‘Steve’ def greet(): globals()[‘name’] = ‘Ram’ name=‘Steve’ print (‘Hello ’, name)
greet()
print(name) #Ram`Try it
The result of the above code shows a conflict between the global and local variables with the same name and how it is resolved.
Visit Globals and Locals in Python for more information. Globals and Locals in Python