今天我们要讲的偏函数,其实是函数的辅佐,什么意思呢,我们借助Python的help帮助函数,看一下

Python3 functools partial 偏函数-新乡seo|网站优化,网站建设—昊天博客
[code lang="shell"]
| partial(func, *args, **keywords) - new function with partial application
| of the given arguments and keywords.
[/code]

[code lang="python"]
# -*- coding: utf-8 -*-
"""
@author:songhao
@file: func.py
@time: 2018/01/03
"""
import functools

# print(dir(functools))
from _operator import add

fc = [x for x in dir(functools) if not x.startswith('_')]
print(fc)

# print(help(functools.partial))

def say(name, age):
return (name, age)

d = functools.partial(say, age=18)

print(d('songhao'))
print(d('songhao',age=28))

# /usr/local/bin/python3 "/Users/songhao/Desktop/python scrapy/func.py"
# ['MappingProxyType', 'RLock', 'WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', 'WeakKeyDictionary', 'cmp_to_key', 'get_cache_token', 'lru_cache', 'namedtuple', 'partial', 'partialmethod', 'recursive_repr', 'reduce', 'singledispatch', 'total_ordering', 'update_wrapper', 'wraps']
# ('songhao', 18)
# ('songhao', 28)

[/code]

functools.partial的作用就是,把一个函数的某些参数给固定住(也就是设置默认值),返回一个新的函数,调用这个新函数会更简单。
冻结参数 把多个函数的部分函数进行默认赋值