99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python變量作用域
Python變量作用域
2017-03-17
收藏

Python變量作用域

1、作用域介紹
python中的作用域分4種情況: L:local,局部作用域,即函數(shù)中定義的變量;
E:enclosing,嵌套的父級函數(shù)的局部作用域,即包含此函數(shù)的上級函數(shù)的局部作用域,但不是全局的;
G:globa,全局變量,就是模塊級別定義的變量; B:built-in,系統(tǒng)固定模塊里面的變量,比如int, bytearray等。 搜索變量的優(yōu)先級順序依次是:作用域局部>外層作用域>當(dāng)前模塊中的全局>python內(nèi)置作用域,也就是LEGB。
x = int(2.9)  # int built-in
 
g_count = 0  # global
def outer():
    o_count = 1  # enclosing
    def inner():
        i_count = 2  # local
當(dāng)然,local和enclosing是相對的,enclosing變量相對上層來說也是local。
2、作用域產(chǎn)生
在Python中,只有模塊(module),類(class)以及函數(shù)(def、lambda)才會引入新的作用域,其它的代碼塊(如if、try、for等)是不會引入新的作用域的,如下代碼:
if True:
    x = 1;
print(x)
# 1
這個是沒有問題的,if并沒有引入一個新的作用域,x仍處在當(dāng)前作用域中,后面代碼可以使用。
def test():
    x2 = 2
print(x2)
# NameError: name 'x2' is not defined
def、class、lambda是可以引入新作用域的。
3、變量的修改
一個不在局部作用域里的變量默認是只讀的,如果試圖為其綁定一個新的值,python認為是在當(dāng)前的局部作用域里創(chuàng)建一個新的變量,也就是說在當(dāng)前局部作用域中,如果直接使用外部作用域的變量,那么這個變量是只讀的,不能修改,如:
count = 10
def outer():
    print(count)
    count = 100
    print(count)
outer()
#UnboundLocalError: local variable 'count' referenced before assignment
這里第一個print中,使用到了外部作用域的count,這樣后面count就指外部作用域中的count了,再修改就會報錯。 如果沒使用過這個變量,而直接賦值,會認為是新定義的變量,此時會覆蓋外部作用域中變量,如:
count = 10
def outer():
    count = 100  
    print(count)
outer()
#100
內(nèi)部作用域中直接聲明了count=100,后面使用count都是內(nèi)部作用域的了。
4、global關(guān)鍵字
當(dāng)內(nèi)部作用域想修改外部作用域的變量時,就要用到global和nonlocal關(guān)鍵字了,當(dāng)修改的變量是在全局作用域(global作用域)上的,就要使用global先聲明一下,代碼如下:
count = 10
def outer():
    global count
    print(count)
    count = 100
    print(count)
outer()
#10
#100
5、nonlocal關(guān)鍵字
global關(guān)鍵字聲明的變量必須在全局作用域上,不能嵌套作用域上,當(dāng)要修改嵌套作用域(enclosing作用域,外層非全局作用域)中的變量怎么辦呢,這時就需要nonlocal關(guān)鍵字了
def outer():
    count = 10
    def inner():
        nonlocal count
        count = 20
        print(count)
    inner()
    print(count)
outer()
#20
#20
6、小結(jié)
(1)變量查找順序:LEGB,作用域局部>外層作用域>當(dāng)前模塊中的全局>python內(nèi)置作用域;
(2)只有模塊、類、及函數(shù)才能引入新作用域;
(3)對于一個變量,內(nèi)部作用域先聲明就會覆蓋外部變量,不聲明直接使用,就會使用外部作用域的變量;
(4)內(nèi)部作用域要修改外部作用域變量的值時,全局變量要使用global關(guān)鍵字,嵌套作用域變量要使用nonlocal關(guān)鍵字。nonlocal是python3新增的關(guān)鍵字,有了這個關(guān)鍵字,就能完美的實現(xiàn)閉包了。數(shù)據(jù)分析師培訓(xùn)

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }