[變數] 使用全域變數

##global variable here##
myvar = 'test1'

def my_func1():
    ##it will print the content of global variable##
    print(myvar)

my_func1()


def my_func2():
    myvar = 'test2'
    ##it will print the content of local variable##
    print(myvar)

my_func2()
##the content of global variable does not change##
print(myvar)


def my_func3():
    ##use global variable in function like this##
    global myvar
    myvar = 'test3'
    print(myvar)

my_func3()
##the content of global variable will be changed in the function##
print(myvar)

留言

這個網誌中的熱門文章

[環境設定] 應用程式執行別名