【每天1分钟】PYTHON基础之函数(偏函数)

【每天1分钟】PYTHON基础之函数(偏函数)

用法:

>>> from functools import partial
>>> help(partial)
Help on class partial in module functools:

class partial(builtins.object)
 |  partial(func, *args, **keywords) - new function with partial application
 |  of the given arguments and keywords.
 |  
 |  Methods defined here:
 |  
 |  __call__(self, /, *args, **kwargs)
 |      Call self as a function.
 |  
 |  __delattr__(self, name, /)
 |      Implement delattr(self, name).
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  __reduce__(...)
 |      helper for pickle
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __setattr__(self, name, value, /)
 |      Implement setattr(self, name, value).
 |  
 |  __setstate__(...)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |  
 |  args
 |      tuple of arguments to future partial calls
 |  
 |  func
 |      function object to use in future partial calls
 |  
 |  keywords
 |      dictionary of keyword arguments to future partial calls

>>> 

简单地说

func 函数名称
args 传入原来函数的元组型参数
keywords 传入原来函数的字典型参数

举例:

>>> from functools import partial
>>> 
>>> def myadd(arg1, arg2):
	return arg1 + arg2

>>> addone = partial(myadd, arg2 = 1)
>>> 
>>> addone(2)
3
>>> addtwo = partial(myadd, 2)
>>> 
>>> addtwo(2)
4
>>> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值