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

熱線電話:13121318867

登錄
首頁精彩閱讀Python中函數(shù)的參數(shù)傳遞與可變長參數(shù)介紹
Python中函數(shù)的參數(shù)傳遞與可變長參數(shù)介紹
2017-10-12
收藏

Python中函數(shù)的參數(shù)傳遞與可變長參數(shù)介紹

這篇文章主要介紹了Python中函數(shù)的參數(shù)傳遞與可變長參數(shù)介紹,本文分別給出多個代碼實例來講解多種多樣的函數(shù)參數(shù),需要的朋友可以參考下


1.Python中也有像C++一樣的默認缺省函數(shù)

代碼如下:
def foo(text,num=0):
    print text,num

foo("asd")  #asd 0
foo("def",100)  #def 100

定義有默認參數(shù)的函數(shù)時,這些默認值參數(shù) 位置必須都在非默認值參數(shù)后面。

調(diào)用時提供默認值參數(shù)值時,使用提供的值,否則使用默認值。

2.Python可以根據(jù)參數(shù)名傳參數(shù)

復(fù)制代碼代碼如下:

def foo(ip,port):
    print "%s:%d" % (ip,port)

foo("192.168.1.0",3306) #192.168.1.0:3306
foo(port=8080,ip="127.0.0.1")   #127.0.0.1:8080


第4行,沒有指定參數(shù)名,按照順序傳參數(shù)。

第5行,指定參數(shù)名,可以按照參數(shù)名稱傳參數(shù)。

3.可變長度參數(shù)

代碼如下:
#coding:utf-8       #設(shè)置python文件的編碼為utf-8,這樣就可以寫入中文注釋
def foo(arg1,*tupleArg,**dictArg):
    print "arg1=",arg1  #formal_args
    print "tupleArg=",tupleArg  #()
    print "dictArg=",dictArg    #[]
foo("formal_args")


上面函數(shù)中的參數(shù),tupleArg前面“*”表示這個參數(shù)是一個元組參數(shù),從程序的輸出可以看出,默認值為();dicrtArg前面有“**”表示這個字典參數(shù)(鍵值對參數(shù))。可以把tupleArg、dictArg看成兩個默認參數(shù)。多余的非關(guān)鍵字參數(shù),函數(shù)調(diào)用時被放在元組參數(shù)tupleArg中;多余的關(guān)鍵字參數(shù),函數(shù)調(diào)用時被放字典參數(shù)dictArg中。

下面是可變長參數(shù)的一些用法:

代碼如下:
#coding:utf-8       #設(shè)置python文件的編碼為utf-8,這樣就可以寫入中文注釋
def foo(arg1,arg2="OK",*tupleArg,**dictArg):
    print "arg1=",arg1
    print "arg2=",arg2
    for i,element in enumerate(tupleArg):
        print "tupleArg %d-->%s" % (i,str(element))
    for  key in dictArg:
        print "dictArg %s-->%s" %(key,dictArg[key])

myList=["my1","my2"]
myDict={"name":"Tom","age":22}
foo("formal_args",arg2="argSecond",a=1)
print "*"*40
foo(123,myList,myDict)
print "*"*40
foo(123,rt=123,*myList,**myDict)

輸出為:

從上面的程序可以看出:

(1)如代碼第16行。

參數(shù)中如果使用“*”元組參數(shù)或者“**”字典參數(shù),這兩種參數(shù)應(yīng)該放在參數(shù)列表最后。并且“*”元組參數(shù)位于“**”字典參數(shù)之前。

關(guān)鍵字參數(shù)rt=123,因為函數(shù)foo(arg1,arg2="OK",*tupleArg,**dictArg)中沒有rt參數(shù),所以最后也歸到字典參數(shù)中。

(2)如代碼第14行。

元組對象前面如果不帶“*”、字典對象如果前面不帶“**”,則作為普通的對象傳遞參數(shù)。

多余的普通參數(shù),在foo(123,myList,myDict)中,123賦給參數(shù)arg1,myList賦給參數(shù)arg2,多余的參數(shù)myDict默認為元組賦給myList。



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