python 高階函數: Partial(偏函數)

來源:互聯網
上載者:User

標籤:title   ESS   xtend   ret   int   hit   err   roc   ges   

格式:

        functools.partail(函數,函數的參數) -------> int(x, base) ----->  functools.partial(int, base)


一、int函數

官網介紹:class int(x, base=10)  #x為字串數字, 預設該字串數字是十進位數, 傳回值是十進位數



#!/usr/bin/python

##普通使用
print "int('10001', 2):", int('10001', 2)  #字串數字‘10001’是2進位數, 傳回值是十進位數


##自訂使用

def int2(x, base=2):
    return int(x, base)
   
print "int2('1010101'):", int2('1010101')


二、partial函數

官網介紹:

  • functools.partial(func[,*args][, **keywords])?

  • Return a new partial object which when called will behave like funccalled with the positional arguments args and keyword arguments keywords. Ifmore arguments are supplied to the call, they are appended to args. Ifadditional keyword arguments are supplied, they extend and override keywords.


import functools  #匯入模組

int3 = functools.partial(int, base=2)  # int3: 將int函數的base參數設定為預設值2進位
print "int3('100'):", int3('100')


int4 = functools.partial(int, ‘2’)  #int4:將int函數的x參數設定為預設值‘2’ ,其base預設為十進位

print "int4('2'):", int4()

python 高階函數: Partial(偏函數)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.