Home >Backend Development >Python Tutorial >How to define global variables in python class

How to define global variables in python class

(*-*)浩
(*-*)浩Original
2019-06-22 11:51:2613231browse

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.

How to define global variables in python class

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 the Python Tutorial column 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn