How to declare a python variable as a global variable
This article is about how python variables are declared as global variables. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Assign variables inside the function and use global line.
Def declare_a_global_variable (): global global_variable_1 global_variable_1 = 1 # Note to use the function to global variablesdeclare_a_global_variable ()
2. Assign values outside the function. If you want to change global line,update_variables () in another function, you should use global before assigning variables.
Global_variable_1 = 1global_variable_2 = 2 def update_variables (): global global_variable_1 global_variable_1 = 11 global_variable_2 = 12 # will update just locally for this function update_variables () print (global_variable_1) # prints 11print (global_variable_2) # prints 2 Thank you for reading! This is the end of this article on "how to declare a python variable as a global variable". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!