国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中使用partial改變方法默認(rèn)參數(shù)實例

2020-02-23 00:56:50
字體:
供稿:網(wǎng)友

Python 標(biāo)準(zhǔn)庫中 functools庫中有很多對方法很有有操作的封裝,partial Objects就是其中之一,他是對方法參數(shù)默認(rèn)值的修改。
下面就看下簡單的應(yīng)用測試。
代碼如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#python2.7x
#partial.py
#authror: orangleliu

'''
functools 中Partial可以用來改變一個方法默認(rèn)參數(shù)
1 改變原有默認(rèn)值參數(shù)的默認(rèn)值
2 給原來沒有默認(rèn)值的參數(shù)增加默認(rèn)值
'''
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)

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凌源市| 台中县| 永胜县| 晋宁县| 浠水县| 长顺县| 修水县| 江津市| 焉耆| 资兴市| 清涧县| 积石山| 吉林市| 明水县| 神池县| 修水县| 鸡东县| 横峰县| 交口县| 马山县| 靖远县| 通海县| 霍邱县| 大理市| 土默特左旗| 浠水县| 江口县| 加查县| 个旧市| 黑水县| 剑川县| 双峰县| 安化县| 阿拉善右旗| 黎平县| 镇远县| 渭源县| 昔阳县| 建德市| 宝丰县| 象山县|