Python3 functools partial 偏函数

2024-01-21 04:08

本文主要是介绍Python3 functools partial 偏函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

Shell
| <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/partial" title="View all posts in partial" target="_blank">partial</a></span>(func, *args, **keywords) - new function with <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/partial" title="View all posts in partial" target="_blank">partial</a></span> application | of the given arguments and keywords.
1
2
| partial ( func , * args , * * keywords ) - new function with partial application
| of the given arguments and keywords .
Python
# -*- coding: utf-8 -*- """ @author:songhao @file: func.py @time: 2018/01/03 """ import <span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/functools" title="View all posts in functools" target="_blank">functools</a></span> # print(dir(<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/functools" title="View all posts in functools" target="_blank">functools</a></span>)) 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/<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/python" title="View all posts in python" target="_blank">python</a></span>3 "/Users/songhao/Desktop/<span class="wp_keywordlink"><a href="http://www.168seo.cn/python" title="python">python</a></span> 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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- 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)

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




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱

这篇关于Python3 functools partial 偏函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/628328

相关文章

pandas使用apply函数给表格同时添加多列

《pandas使用apply函数给表格同时添加多列》本文介绍了利用Pandas的apply函数在DataFrame中同时添加多列,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录一、Pandas使用apply函数给表格同时添加多列二、应用示例一、Pandas使用apply函

python3中正则表达式处理函数用法总结

《python3中正则表达式处理函数用法总结》Python中的正则表达式是一个强大的文本处理工具,用于匹配、查找、替换等操作,在Python中正则表达式的操作主要通过内置的re模块来实现,这篇文章主要... 目录前言re.match函数re.search方法re.match 与 re.search的区别检索

Python中Namespace()函数详解

《Python中Namespace()函数详解》Namespace是argparse模块提供的一个类,用于创建命名空间对象,它允许通过点操作符访问数据,比字典更易读,在深度学习项目中常用于加载配置、命... 目录1. 为什么使用 Namespace?2. Namespace 的本质是什么?3. Namesp

MySQL中如何求平均值常见实例(AVG函数详解)

《MySQL中如何求平均值常见实例(AVG函数详解)》MySQLavg()是一个聚合函数,用于返回各种记录中表达式的平均值,:本文主要介绍MySQL中用AVG函数如何求平均值的相关资料,文中通过代... 目录前言一、基本语法二、示例讲解1. 计算全表平均分2. 计算某门课程的平均分(例如:Math)三、结合

Python函数作用域与闭包举例深度解析

《Python函数作用域与闭包举例深度解析》Python函数的作用域规则和闭包是编程中的关键概念,它们决定了变量的访问和生命周期,:本文主要介绍Python函数作用域与闭包的相关资料,文中通过代码... 目录1. 基础作用域访问示例1:访问全局变量示例2:访问外层函数变量2. 闭包基础示例3:简单闭包示例4

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

python中的高阶函数示例详解

《python中的高阶函数示例详解》在Python中,高阶函数是指接受函数作为参数或返回函数作为结果的函数,下面:本文主要介绍python中高阶函数的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录1.定义2.map函数3.filter函数4.reduce函数5.sorted函数6.自定义高阶函数

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

Python Excel 通用筛选函数的实现

《PythonExcel通用筛选函数的实现》本文主要介绍了PythonExcel通用筛选函数的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录案例目的示例数据假定数据来源是字典优化:通用CSV数据处理函数使用说明使用示例注意事项案例目的第一