1 global1 = "1" 2 3 def o(): 4 #global global1; 5 global1=2; 6 print(global1) 7 8 def p(): 9 print(global1) 10 11 if __name__=="__main__": 12 o() 13 p()
輸出:
2
1
如果第4行不註釋,就是在宣告要對全域性變數進行修改,輸出為:
2
2
1 global1 = "1" 2 3 def o(): 4 #global global1; 5 global1=2; 6 print(global1) 7 8 def p(): 9 print(global1) 10 11 if __name__=="__main__": 12 o() 13 p()
輸出:
2
1
如果第4行不註釋,就是在宣告要對全域性變數進行修改,輸出為:
2
2