Global variables are a common variable in programming languages. Through global definition, they can be created by an object function or anywhere in the program. They can be referenced by all objects or functions in the program. Global variables The definition is conducive to program variable sharing, simplifying the addition and modification of programs.
Like the C language, Python also has global variables. There are two ways to define global variables:
Declaration method(Recommended learning:Python video tutorial)
This method is to directly define and declare global variables in the current module, use the global declaration method, and then reference it!
OLD_URL='http://oldboyedu.com' def bb(): global OLD_URL OLD_URL = OLD_URL +'#m' if __name__=='__main__': bb() print OLD_URL
Module method
This method is to define global variables in a separate module, and then import the defined global variable module into the global module that needs to be used
#gl.py 全局变量模块定义 GL_A=‘hello’ GL_B=’world’ =============== #test.py 全局变量引用模块 import gl def hello_world() print gl. GL_A, GL_B
For more Python-related technical articles, please visit thePython Tutorialcolumn to learn!
The above is the detailed content of How to define global variables in python class. For more information, please follow other related articles on the PHP Chinese website!