When modifying the global variables of a function , Is it necessary to use global Make a statement , It depends on whether the direction of the global variable has been modified . If modified , Must use global Affirming .
Simply speaking : The use of immutable types must be stated , The use of variable types need not be declared .
for example :
Immutable type ( Numbers 、 character string 、 Tuples 、 Immutable set )
Variable type ( list 、 Dictionaries 、 Variable set )
a=1
b=[1]
def demo1():
global a
a+=1
def demo2():
b.append(2)
if __name__ == '__main__':
demo1()
demo2()
print(a, b)