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

熱線電話:13121318867

登錄
首頁精彩閱讀深入理解Python中變量賦值的問題
深入理解Python中變量賦值的問題
2017-08-13
收藏

深入理解Python中變量賦值的問題

Python中變量名規(guī)則與其他大多數(shù)高級語言一樣,都是受C語言影響的,另外變量名是大小寫敏感的。
Python是動態(tài)類型語言,也就是說不需要預(yù)先聲明變量類型,變量的類型和值在賦值那一刻被初始化,下面詳細介紹了Python的變量賦值問題,一起來學習學習吧。

我們先看一下如下代碼:

c = {}
 
def foo():
 f = dict(zip(list("abcd"), [1, 2 ,3 ,4]))
 c.update(f)
 
if __name__ == "__main__":
 a = b = d = c
 
 b['e'] = 5
 d['f'] = 6
 
 foo()
 
 print(a)
 print(b)
 print(c)
 print(d)

輸出結(jié)果:
    
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}

{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4, 'f': 6}


如果你對以上輸出結(jié)果不感到奇怪,那么就不必往下看了。實際上本文要討論的內(nèi)容非常簡單,不要為此浪費您寶貴的時間。

Python屬于動態(tài)語言,程序的結(jié)構(gòu)可以在運行的過程中隨時改變,而且 python 還是弱類型的語言,所以如果你是從靜態(tài)、強類型編程語言轉(zhuǎn)過來的,理解起Python的賦值,剛開始可能會感覺有些代碼有點莫名其妙。

可能你會以為上面代碼的輸出會是這樣的:

{}
{'e': 5}
{}
{'f': 6}



你可能認為 a 沒有被改變,因為沒有看到哪里對它做了改變;b 和 d 的改變是和明顯的;c 呢,因為是在函數(shù)內(nèi)被改變的,你可能認為 c 會是一個局部變量,所以全局的 c 不會被改變。

實際上,這里的 a, b, c, d 同時指向了一塊內(nèi)存空間,這可內(nèi)存空間保存的是一個字典對象。這有點像 c 語言的指針,a, b, c, d 四個指針指向同一個內(nèi)存地址,也就是給這塊內(nèi)存其了 4 個筆名。所以,不管你改變誰,其他三個變量都會跟著變化。那為什么 c 在函數(shù)內(nèi)部被改變,而且沒有用 global 申明,但全局的 c 去被改變了呢?

我們再來看一個例子:

>>>a = {1:1, 2:2}
>>>b = a
>>>a[3] = 3
>>>b
{1: 1, 2: 2, 3: 3}
>>>a = 4
>>>b
{1: 1, 2: 2, 3: 3}
>>>a
4



當 b = a 時,a 與 b 指向同一個對象,所以在 a 中添加一個元素時,b 也發(fā)生變化。而當 a = 4 時, a 就已經(jīng)不再指向字典對象了,而是指向一個新的 int 對象(python 中整數(shù)也是對象),這時只有 b 指向字典,所以 a 改變時 b 沒有跟著變化。這是只是說明了什么時候賦值變量會發(fā)生質(zhì)的改變,而以上的問題還沒有被解決。

那么,我么再來看一個例子:

class TestObj(object):
 pass
 
x = TestObj()
x.x = 8
d = {"a": 1, "b": 2, "g": x}
xx = d.get("g", None)
xx.x = 10
print("x.x:%s" % x.x)
print("xx.x: %s" % xx.x)
print("d['g'].x: %s" % d['g'].x)
 
# Out:
# x.x:10
# xx.x: 10
# d['g'].x: 10



由以上的實例可以了解到,如果僅改變對象的屬性(或者說成是改變結(jié)構(gòu)),所有指向該對象的變量都會隨之改變。但是如果一個變量重新指向了一個對象,那么其他指向該對象的變量不會隨之變化。所以,最開始的例子中,c 雖然在函數(shù)內(nèi)部被改變,但是 c 是全局的變量,我們只是在 c 所指向的內(nèi)存中添加了一個值,而沒有將 c 指向另外的變量。

需要注意的是,有人可能會認為上例中的最后一個輸出應(yīng)該是 d['g'].x: 8。 這樣理解的原因可能是覺得已經(jīng)把字典中 ‘g' 所對應(yīng)的值取出來了,并重新命名為 xx,那么 xx 就與字典無關(guān)了。其實際并不是這樣的,字典中的 key 所對應(yīng)的 value 就像是一個指針指向了一片內(nèi)存區(qū)域,訪問字典中 key 時就是去該區(qū)域取值,如果將值取出來賦值給另外一個變量,例如 xx = d['g'] 或者 xx = d.get("g", None),這樣只是讓 xx 這個變量也指向了該區(qū)域,也就是說字典中的鍵 ‘g' 和 xx 對象指向了同一片內(nèi)存空間,當我們只改變 xx 的屬性時,字典也會發(fā)生變化。

下例更加直觀的展示了這一點:

class TestObj(object):
 pass
 
x = TestObj()
x.x = 8
d = {"a": 1, "b": 2, "g": x}
print(d['g'].x)
xx = d["g"]
xx.x = 10
print(d['g'].x)
xx = 20
print(d['g'].x)
 
# Out:
# 8
# 10
# 10


這個知識點非常簡單,但如果沒有理解,可能無法看明白別人的代碼。這一點有時候會給程序設(shè)計帶來很大的便利,例如設(shè)計一個在整個程序中保存狀態(tài)的上下文:


classContext(object):
 pass
 
 
deffoo(context):
 context.a=10
 context.b=20
 x=1
 
defhoo(context):
 context.c=30
 context.d=40
 x=1
 
if__name__=="__main__":
 context=Context()
 x=None
 foo(context)
 hoo(context)
 print(x)
 print(context.a)
 print(context.b)
 print(context.c)
 print(context.d)
 
# Out:
# None
# 10
# 20
# 30
# 40

示例中我們可以把需要保存的狀態(tài)添加到 context 中,這樣在整個程序的運行過程中這些狀態(tài)能夠被任何位置被使用。

在來一個終結(jié)的例子,執(zhí)行外部代碼:

outer_code.py



from__future__importprint_function
 
definitialize(context):
 g.a=333
 g.b=666
 context.x=888
 
defhandle_data(context, data):
 g.c=g.a+g.b+context.x+context.y
 a=np.array([1,2,3,4,5,6])
 print("outer space: a is %s"%a)
 print("outer space: context is %s"%context)

main_exec.py

from__future__importprint_function
 
importsys
importimp
frompprintimportpprint
 
classContext(object):
 pass
 
classPersistentState(object):
 pass
 
 
# Script starts from here
 
if__name__=="__main__":
 outer_code_moudle=imp.new_module('outer_code')
 outer_code_moudle.__file__='outer_code.py'
 sys.modules["outer_code"]=outer_code_moudle
 outer_code_scope=code_scope=outer_code_moudle.__dict__
 
 head_code="import numpy as np\nfrom main_exec import PersistentState\ng=PersistentState()"
 exec(head_code, code_scope)
 origin_global_names=set(code_scope.keys())
 
 withopen("outer_code.py","rb") as f:
 outer_code=f.read()
 
 import__future__
 code_obj=compile(outer_code,"outer_code.py","exec", flags=__future__.unicode_literals.compiler_flag)
 exec(code_obj, code_scope)
 # 去除掉內(nèi)建名字空間的屬性,僅保留外部代碼中添加的屬性
 outer_code_global_names=set(outer_code_scope.keys())-origin_global_names
 
 outer_func_initialize=code_scope.get("initialize",None)
 outer_func_handle_data=code_scope.get("handle_data",None)
 
 context=Context()
 context.y=999
 outer_func_initialize(context)
 outer_func_handle_data(context,None)
 
 g=outer_code_scope["g"]
 assertg.c==2886
 print("g.c: %s"%g.c)
 print(dir(g))
 print(dir(context))
 pprint(outer_code_moudle.__dict__)

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助



數(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); }