Variables that are created outside of a function (as in all of the examples above) are known as global variables. Show
Global variables can be used by everyone, both inside of functions and outside. ExampleCreate a variable outside of a function, and use it inside the function x = "awesome" def myfunc(): myfunc() Try it Yourself »If you create a variable with the same name inside a function, this variable will be local, and can only be used inside the function. The global variable with the same name will remain as it was, global and with the original value. ExampleCreate a variable inside a function, with the same name as the global variable x = "awesome" def myfunc(): myfunc() print("Python is " + x) Try it Yourself »Learn to Filter Data in Python Like a Data AnalystTry a hands-on training sessions with step-by-step guidance from an expert. Try the guided project made in collaboration with Coursera now! Get startedThe global KeywordNormally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the ExampleIf you use the def myfunc(): myfunc() print("Python is " + x) Try it Yourself »Also, use the ExampleTo change the value of a global variable inside a function, refer to the variable by using the Setting up a variable in the module will work as a global variable you can define multiple variables in the class. The reason for this is that python treats classes as modules. Python global multiple variables exampleSimple example code defines multiple global variables from inside a class. This is the best way to set up a list of global variables would be to set up a class for them in that module. You can call them, update them, etc. in any other function by referencing them as such: main.py
Test.py
Output: Note: The Source: stackoverflow.com Do comment if you have any doubts or suggestions on this Python variable tutorial.
Rohit Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical. In Python, a variable declared outside the function or in global scope is known as a global variable. We can use global variables both inside and outside the function. The scope of a global variable is broad. It is accessible in all functions of the same module. Python global variablesLet’s understand it with an example. Example: In this example, we declared a global variable 2 with the value ‘Jessa’. The same global variable 2 is accessible to everyone, both inside of functions and outside.
Output: Name inside function: Jessa Name Outside function: Jessa Using Global Variables In FunctionWe can use global variables across multiple functions of the same module. Now, let’s see how to use the global variable inside a Python function.
Example:
Output: local variable y= 30 global variable x= 20 x+y= 50 local variable m= 10 global variable x= 20 x-m= 10 Global Variable and Local Variable with Same NameNote: If you create a new local variable inside a function with the same name as a global variable, it will not override the value of a global variable. Instead, the new variable will be local and can only be used inside the function. The global variable with the same name will remain unchanged. Example:
Output: local variable x= 50 local variable x= 100 Global variable x= 20 # global variable x = 20 def add(): # local variable y y = 30 print('local variable y=', y) # Use global variable x print('global variable x=', x) z = x + y print('x+y=', z) def sub(): # local variable m m = 10 print('local variable m=', m) # Use global variable x in second function print('global variable x=', x) n = x - m print('x-m=', n) add() sub()0 Keyword in PythonThe 0 keyword is used in the following two cases.
Modify a global variable inside a functionlet’s see the below code.
Output: UnboundLocalError: local variable 'x' referenced before assignment Execute the above code to change the global variable x’s value. You’ll get an 9 because Python treats 4 as a local variable, and 4 is also not defined inside my_func(). i.e, You cannot change or reassign value to a global variable inside a function just like this.Use the 0 keyword to change the value of a global variable inside a function.Example:
Output: global variable x outside a function: 20 global variable x inside a function: 50 global variable x outside a function: 50 Create a global variable inside a functionin Python, the scope of variables created inside a function is limited to that function. We cannot access the local variables from outside of the function. Because the scope is local, those variables are not visible outside the function. To overcome this limitation, we can use the 0 keyword to create a global variable inside a function. This global variable is accessible within and outside the function.Example: Name inside function: Jessa Name Outside function: Jessa0 Output: Name inside function: Jessa Name Outside function: Jessa1 Rules of # global variable x = 20 def add(): # local variable y y = 30 print('local variable y=', y) # Use global variable x print('global variable x=', x) z = x + y print('x+y=', z) def sub(): # local variable m m = 10 print('local variable m=', m) # Use global variable x in second function print('global variable x=', x) n = x - m print('x-m=', n) add() sub()0 keywordLet us see the rules we need to follow to create and use a global keyword.
Global Variables Across Python Modules/FilesBy default, the global variables are accessible across multiple functions of the same module. Now, we’ll see how to share global variables across the modules.
Let us understand it using an example. In Python, to create a module, write Python code in the file, and save that file with the local variable y= 30 global variable x= 20 x+y= 50 local variable m= 10 global variable x= 20 x-m= 106 extension. Example: Share global variables across Python modules. local variable y= 30 global variable x= 20 x+y= 50 local variable m= 10 global variable x= 20 x-m= 105: The config module stores global variables of school and grade Name inside function: Jessa Name Outside function: Jessa2 Now, run the local variable y= 30 global variable x= 20 x+y= 50 local variable m= 10 global variable x= 20 x-m= 105 file. local variable y= 30 global variable x= 20 x+y= 50 local variable m= 10 global variable x= 20 x-m= 109: create a local variable y= 30 global variable x= 20 x+y= 50 local variable m= 10 global variable x= 20 x-m= 109 file to import global variables and modify them. In the local variable y= 30 global variable x= 20 x+y= 50 local variable m= 10 global variable x= 20 x-m= 109 file, we import the config.py module and modify the values of the name and address. Name inside function: Jessa Name Outside function: Jessa3 Now, run the local variable y= 30 global variable x= 20 x+y= 50 local variable m= 10 global variable x= 20 x-m= 109 file. 3:Now, in the 4 file, we import both 5 and 6 modules to test the values of global variables and whether they are changed.Name inside function: Jessa Name Outside function: Jessa4 Output: Name inside function: Jessa Name Outside function: Jessa5 As you can see in the output, we successfully accessed and modified the global variables across the files or modules. globals() function in PythonIn this section, we’ll see what the globals() do in Python. We can also use the 1 function to access and modify the global variables. The 1 function returns the dictionary of the current global symbol table.The global symbol table stores all information related to the program’s global scope and is accessed using the 1 method.Function and variables are not part of any class, or functions are stored in a global symbol table. Example: Modify the global variable using the 1 function.Name inside function: Jessa Name Outside function: Jessa6 Output: Name inside function: Jessa Name Outside function: Jessa7 Global variables in Nested FunctionNow, Let’s see how to use a global variable in a nested function. Global variables can be used in a nested function using global or nonlocal keywords. The difference between nonlocal and global is that global is used to change global variables, while nonlocal is used to change variables outside the function. Let us illustrate this with an example. |