日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python中使用partial改變方法默認參數實例

系統 1776 0

Python 標準庫中 functools庫中有很多對方法很有有操作的封裝,partial Objects就是其中之一,他是對方法參數默認值的修改。
下面就看下簡單的應用測試。

復制代碼 代碼如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#python2.7x
#partial.py
#authror: orangleliu

'''
functools 中Partial可以用來改變一個方法默認參數
1 改變原有默認值參數的默認值
2 給原來沒有默認值的參數增加默認值
'''
def foo(a,b=0) :
??? '''
??? int add'
??? '''
??? print a + b

#user default argument
foo(1)

#change default argument once
foo(1,1)

#change function's default argument, and you can use the function with new argument
import functools

foo1 = functools.partial(foo, b=5)? #change "b" default argument
foo1(1)

foo2 = functools.partial(foo, a=10) #give "a" default argument
foo2()

'''
foo2 is a partial object,it only has three read-only attributes
i will list them
'''
print foo2.func
print foo2.args
print foo2.keywords
print dir(foo2)

##默認情況下partial對象是沒有 __name__ __doc__ 屬性,使用update_wrapper 從原始方法中添加屬性到partial 對象中
print foo2.__doc__
'''
執行結果:
partial(func, *args, **keywords) - new function with partial application
??? of the given arguments and keywords.
'''

functools.update_wrapper(foo2, foo)
print foo2.__doc__
'''
修改為foo的文檔信息了
'''


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 敖汉旗| 景德镇市| 舒城县| 双江| 岳阳县| 县级市| 青浦区| 留坝县| 琼结县| 彭山县| 菏泽市| 牡丹江市| 牙克石市| 达拉特旗| 盐边县| 东丽区| 贵港市| 滦平县| 武平县| 玉门市| 高淳县| 洛宁县| 聂荣县| 永宁县| 游戏| 梅河口市| 巴东县| 黄冈市| 雷州市| 彝良县| 天祝| 奇台县| 安多县| 遂溪县| 利川市| 蒙城县| 金塔县| 揭阳市| 奉新县| 双江| 三明市|