python-global全域變數

來源:互聯網
上載者:User

標籤:若是   cal   bsp   變化   關係   oba   hang   bar   aced   

在函數內部定義變數時,他們與函數外部具有相同名稱的其他變數沒有任何關係,即變數名稱對於函數來說是局部的,這稱為變數的範圍,樣本如下:

def func_local(x):    print ‘x is‘, x    x = 2    print ‘Chanaged local x to‘,xx = 50func_local(x)print ‘x is still‘, x

 執行結果:

x is 50Chanaged local x to 2x is still 50

如果想在函數內部改變函數外的變數值,用global陳述式完成

def func_global():    global y    print ‘y is‘, y    y = 50    print ‘Changed local y to‘, yy = 10func_global()print ‘Value of y is‘, y

def func_global():    global y    print ‘y is‘, y    y = 50    print ‘Changed local y to‘, yy = 10func_global()print ‘Value of y is‘, y

執行結果:

y is 10Changed local y to 50Value of y is 50

y is 10Changed local y to 50Value of y is 50

函數參數若是list、set、dict可變參數,在函數內改變參數,會導致該參數發生變化,例如:

def func_local(x):    print ‘x is‘, x    x.append(10)    print ‘Chanaged local x to‘,xx = range(6)func_local(x)print ‘x is‘, x

def func_local(x):    print ‘x is‘, x    x.append(10)    print ‘Chanaged local x to‘,xx = range(6)func_local(x)print ‘x is‘, x

執行結果

x is [0, 1, 2, 3, 4, 5]Chanaged local x to [0, 1, 2, 3, 4, 5, 10]x is [0, 1, 2, 3, 4, 5, 10]

x is [0, 1, 2, 3, 4, 5]Chanaged local x to [0, 1, 2, 3, 4, 5, 10]x is [0, 1, 2, 3, 4, 5, 10]
def func_local(x):    print ‘x is‘, x    x.add(10)    print ‘Chanaged local x to‘,xx = set(range(6))func_local(x)print ‘x is‘, x

def func_local(x):    print ‘x is‘, x    x.add(10)    print ‘Chanaged local x to‘,xx = set(range(6))func_local(x)print ‘x is‘, x

執行結果:

x is set([0, 1, 2, 3, 4, 5])Chanaged local x to set([0, 1, 2, 3, 4, 5, 10])x is set([0, 1, 2, 3, 4, 5, 10])

x is set([0, 1, 2, 3, 4, 5])Chanaged local x to set([0, 1, 2, 3, 4, 5, 10])x is set([0, 1, 2, 3, 4, 5, 10])
def func_local(x):    print ‘x is‘, x    x[‘x‘] = 2    print ‘Chanaged local x to‘,xx = dict([(‘x‘,1), (‘y‘, 2)])func_local(x)print ‘x is‘, x

def func_local(x):    print ‘x is‘, x    x[‘x‘] = 2    print ‘Chanaged local x to‘,xx = dict([(‘x‘,1), (‘y‘, 2)])func_local(x)print ‘x is‘, x

執行結果:

x is {‘y‘: 2, ‘x‘: 1}Chanaged local x to {‘y‘: 2, ‘x‘: 2}x is {‘y‘: 2, ‘x‘: 2}

x is {‘y‘: 2, ‘x‘: 1}Chanaged local x to {‘y‘: 2, ‘x‘: 2}x is {‘y‘: 2, ‘x‘: 2}

def func_local(x):    print ‘x is‘, x    x = (4, 5, 6)    print ‘Chanaged local x to‘,xx = (1,2,3,)func_local(x)print ‘x is‘, x
def func_local(x):    print ‘x is‘, x    x = (4, 5, 6)    print ‘Chanaged local x to‘,xx = (1,2,3,)func_local(x)print ‘x is‘, x

執行結果

x is (1, 2, 3)Chanaged local x to (4, 5, 6)x is (1, 2, 3)
x is (1, 2, 3)Chanaged local x to (4, 5, 6)x is (1, 2, 3)

若傳入可變參數如list、set、dict,在函數內部對參數做出修改,參數本身發生變化,tuple、str不變

 

python-global全域變數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.