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

熱線電話:13121318867

登錄
首頁精彩閱讀Python函數(shù)參數(shù)類型*、**的區(qū)別
Python函數(shù)參數(shù)類型*、**的區(qū)別
2017-10-07
收藏

Python函數(shù)參數(shù)類型*、**的區(qū)別

剛開始學(xué)習(xí)python,python相對于java確實要簡潔易用得多。內(nèi)存回收類似hotspot的可達(dá)性分析, 不可變對象也如同java得Integer類型,with函數(shù)類似新版本C++的特性,總體來說理解起來比較輕松。只是函數(shù)部分參數(shù)的"*"與"**",閉包等問題,著實令人迷糊了一把,弄清概念后寫下此文記錄下來,也希望本文能夠幫助其他初學(xué)者。

所以本文是一篇學(xué)習(xí)筆記,著重于使用的細(xì)節(jié)和理解上,首先分別介紹了函數(shù)各種參數(shù)類型在調(diào)用和聲明時的區(qū)別,及其在混用時需要注意的一些細(xì)節(jié),之后講了閉包相關(guān)的內(nèi)容。如果有不對的地方歡迎指正。

函數(shù)參數(shù)不帶“*”,"*" 與 "**"的區(qū)別
理解這個問題得關(guān)鍵在于要分開理解調(diào)用和聲明語法中3者得區(qū)別.

函數(shù)調(diào)用區(qū)別

1. 不同類型的參數(shù)簡述
#這里先說明python函數(shù)調(diào)用得語法為:


舉個例子來說明這4種調(diào)用方式得區(qū)別:

代碼如下:
#-------------------------------
#positional_args方式
>>> test(1,2,3,4,5)
1 2 3 4 5

#這種調(diào)用方式的函數(shù)處理等價于
a,b,c,d,e = 1,2,3,4,5
print a,b,c,d,e

#-------------------------------
#keyword_args方式
>>> test(a=1,b=3,c=4,d=2,e=1)
1 3 4 2 1

#這種處理方式得函數(shù)處理等價于
a=1
b=3
c=4
d=2
e=1
print a,b,c,d,e

#-------------------------------
#*tuple_grp_nonkw_args方式
>>> x = 1,2,3,4,5
>>> test(*x)
1 2 3 4 5


#這種方式函數(shù)處理等價于

代碼如下:
a,b,c,d,e = x
print a,b,c,d,e
#特別說明:x也可以為dict類型,x為dick類型時將鍵傳遞給函數(shù)
>>> y
{'a': 1, 'c': 6, 'b': 2, 'e': 1, 'd': 1}
>>> test(*y)
a c b e d

#---------------------------------
#**dict_grp_kw_args方式
>>> y
{'a': 1, 'c': 6, 'b': 2, 'e': 1, 'd': 1}
>>> test(**y)
1 2 6 1 1

#這種函數(shù)處理方式等價于
a = y['a']
b = y['b']
... #c,d,e不再贅述
print a,b,c,d,e

2. 不同類型參數(shù)混用需要注意的一些細(xì)節(jié)
  接下來說明不同參數(shù)類型混用的情況,要理解不同參數(shù)混用得語法需要理解以下幾方面內(nèi)容.

首先要明白,函數(shù)調(diào)用使用參數(shù)類型必須嚴(yán)格按照順序,不能隨意調(diào)換順序,否則會報錯. 如 (a=1,2,3,4,5)會引發(fā)錯誤,; (*x,2,3)也會被當(dāng)成非法.

其次,函數(shù)對不同方式處理的順序也是按照上述的類型順序.因為#keyword_args方式和**dict_grp_kw_args方式對參數(shù)一一指定,所以無所謂順序.所以只需要考慮順序賦值(positional_args)和列表賦值(*tuple_grp_nonkw_args)的順序.因此,可以簡單理解為只有#positional_args方式,#*tuple_grp_nonkw_args方式有邏輯先后順序的.

最后,參數(shù)是不允許多次賦值的.

舉個例子說明,順序賦值(positional_args)和列表賦值(*tuple_grp_nonkw_args)的邏輯先后關(guān)系:

代碼如下:
#只有在順序賦值,列表賦值在結(jié)果上存在羅輯先后關(guān)系
#正確的例子1
>>> x = {3,4,5}
>>> test(1,2,*x)
1 2 3 4 5
#正確的例子2
>>> test(1,e=2,*x)
1 3 4 5 2

#錯誤的例子
>>> test(1,b=2,*x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: test() got multiple values for keyword argument 'b'

#正確的例子1,處理等價于
a,b = 1,2 #順序參數(shù)
c,d,e = x #列表參數(shù)
print a,b,c,d,e

#正確的例子2,處理等價于
a = 1 #順序參數(shù)
e = 2 #關(guān)鍵字參數(shù)
b,c,d = x #列表參數(shù)

#錯誤的例子,處理等價于
a = 1 #順序參數(shù)
b = 2 #關(guān)鍵字參數(shù)
b,c,d = x #列表參數(shù)
#這里由于b多次賦值導(dǎo)致異常,可見只有順序參數(shù)和列表參數(shù)存在羅輯先后關(guān)系

函數(shù)聲明區(qū)別

理解了函數(shù)調(diào)用中不同類型參數(shù)得區(qū)別之后,再來理解函數(shù)聲明中不同參數(shù)得區(qū)別就簡單很多了.

1. 函數(shù)聲明中的參數(shù)類型說明

函數(shù)聲明只有3種類型, arg, *arg , **arg 他們得作用和函數(shù)調(diào)用剛好相反. 調(diào)用時*tuple_grp_nonkw_args將列表轉(zhuǎn)換為順序參數(shù),而聲明中的*arg的作用是將順序賦值(positional_args)轉(zhuǎn)換為列表. 調(diào)用時**dict_grp_kw_args將字典轉(zhuǎn)換為關(guān)鍵字參數(shù),而聲明中**arg則反過來將關(guān)鍵字參數(shù)(keyword_args)轉(zhuǎn)換為字典.
特別提醒:*arg 和 **arg可以為空值.

以下舉例說明上述規(guī)則:

代碼如下:
#arg, *arg和**arg作用舉例
def test2(a,*b,**c):
 print a,b,c
#---------------------------
#*arg 和 **arg可以不傳遞參數(shù)
>>> test2(1)
1 () {}
#arg必須傳遞參數(shù)
>>> test2()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: test2() takes at least 1 argument (0 given)

#----------------------------
#*arg將順positional_args轉(zhuǎn)換為列表
>>> test2(1,2,[1,2],{'a':1,'b':2})
1 (2, [1, 2], {'a': 1, 'b': 2}) {}
#該處理等價于
a = 1 #arg參數(shù)處理
b = 2,[1,2],{'a':1,'b':2} #*arg參數(shù)處理
c = dict() #**arg參數(shù)處理
print a,b,c

#-----------------------------
#**arg將keyword_args轉(zhuǎn)換為字典
>>> test2(1,2,3,d={1:2,3:4}, c=12, b=1)
1 (2, 3) {'c': 12, 'b': 1, 'd': {1: 2, 3: 4}}
#該處理等價于
a = 1 #arg參數(shù)處理
b= 2,3 #*arg參數(shù)處理
#**arg參數(shù)處理
c = dict()
c['d'] = {1:2, 3:4}
c['c'] = 12
c['b'] = 1
print a,b,c

2. 處理順序問題

函數(shù)總是先處理arg類型參數(shù),再處理*arg和**arg類型的參數(shù). 因為*arg和**arg針對的調(diào)用參數(shù)類型不同,所以不需要考慮他們得順序.

代碼如下:
def test2(a,*b,**c):
 print a,b,c
>>> test2(1, b=[1,2,3], c={1:2, 3:4},a=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: test2() got multiple values for keyword argument 'a'
#這里會報錯得原因是,總是先處理arg類型得參數(shù)
#該函數(shù)調(diào)用等價于
#處理arg類型參數(shù):
a = 1
a = 1  #多次賦值,導(dǎo)致異常
#處理其他類型參數(shù)
...
print a,b,c


閉包
  python的函數(shù),原本只能訪問兩個區(qū)域的變量:全局,和局部(函數(shù)上下文). 實際上,函數(shù)本身也是一個對象,也有自己的作用域. 閉包通過函數(shù)與引用集合的組合,使得函數(shù)可以在它被定義的區(qū)域之外執(zhí)行. 這個集合可以通過func_closure來獲取這個引用集合. 這與python處理全局變量得方式一樣,只不過全局變量將引用集合存儲在__globals__字段中.func_closure是一個存儲cell類型的元組,每個cell存儲一個上下文變量.

另外,舊版本得python的內(nèi)部函數(shù)不能在其他作用域使用的原因,并不是因為每個作用域的變量嚴(yán)格相互隔離,而是脫離原本的作用域后,函數(shù)失去了原本上下文的引用。需要注意的是,閉包存儲的上下文信息一樣是淺拷貝,所以傳遞給內(nèi)部函數(shù)的可變對象仍然會被其他擁有該對象引用得變量修改.

舉個例子:

代碼如下:

>>> def foo(x,y):
...     def bar():
...             print x,y
...     return bar
...
#查看func_closure的引用信息
>>> a = [1,2]
>>> b = foo(a,0)
>>> b.func_closure[0].cell_contents
[1, 2]
>>> b.func_closure[1].cell_contents
0
>>> b()
[1, 2] 0 #可變對象仍然能被修改
>>> a.append(3)
>>> b.func_closure[0].cell_contents
[1, 2, 3]
>>> b()
[1, 2, 3] 0

數(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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(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ù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) 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); }