Python's global variable usage

Wrong approach:

         For example, define the global variable CONSTANT as follows. Then operate it directly in the function. I have always written C before, so I am used to defining it like this

#encoding=utf-8
import numpy as np 
CONSTANT = 0
def function(BINARY,himg ,wimg,txtname):
    CONSTANT += 1

        The result will show that the variable CONSTANT is not defined.

Correct way:

        Use global to modify this keyword. Can be used as a global variable

#encoding=utf-8
import numpy as np 
CONSTANT = 0
def function(BINARY,himg ,wimg,txtname):
    global CONSTANT
    CONSTANT += 1

 

Guess you like

Origin blog.csdn.net/gbz3300255/article/details/108746593