Python基础-偏函数 Partial function
来源:互联网 发布:linux 邮件系统 编辑:程序博客网 时间:2024/05/19 11:48
偏函数(Partial function)
functools.partial的作用就是,把一个函数的某些参数给固定住(也就是设置默认值),返回一个新的函数,调用这个新函数会更简单。
即类似设计模式的外观模式,让调用接口更加简便,调用更简单
示例
functools.partial(函数对象,*args, **kw)
#!/usr/bin/env python3# -*- coding: utf-8 -*-# 偏函数# 二进制转换def int2(x,base=2): return int(x, base)# 八进制转换def int8(x,base=8): return int(x, base)# 十六进制转换def int8(x,base=16): return int(x, base)# 没有偏函数的时代def nonePartialTest(): print(int2("10110001010")) print(int2("1011000010"))# 有偏函数的时代import functools# 让写法更加简洁def partialTest(): # functools.partial(函数对象,*args, **kw) partialInt2 = functools.partial(int, base=2) print(partialInt2("10110001010")) print(partialInt2("1011000010"))# 同时运行测试函数nonePartialTest()partialTest()
运行结果
D:\PythonProject>python Run.py14187061418706
阅读全文
0 0
- Python基础-偏函数 Partial function
- Python教程学习简记12--Python Partial function 偏函数
- 偏函数(Partial-Function)
- python偏函数partial
- python partial function
- Scala之偏函数Partial Function
- Scala之偏函数Partial Function
- 【python】偏函数 functools partial
- python 中 partial function 骗函数的使用介绍
- Scala: 偏函数(PartialFunction) && 偏应用函数(Partial Applied Function)
- 飘逸的python - 偏函数functools.partial
- 飘逸的python - 偏函数functools.partial
- python进阶8:偏函数functools.partial
- python 中偏函数 partial 的使用
- python 中偏函数 partial 的使用
- python 偏函数(functoosl, partial)
- python的偏函数应用partial
- python中的partial函数
- 机器学习——开发环境
- thinking-in-java(16) 数组
- 架构师之路-创业互联网公司如何搭建自己的技术架构
- 我不想成为咸鱼系列之字符串创建方式及内存的简单分析
- 蓝牙5.0 mesh技术分享(一)
- Python基础-偏函数 Partial function
- Java中的Atomic包使用指南
- Tomcat服务器遇到的异常(一)
- 普通程序员如何向人工智能方向转型?
- 在windows上搭建redis集群(redis-cluster)
- 在树莓派上安装go环境很简单
- Android版本更新(Service下载 Notification进度条:直接拿来用)
- 使用VMware workstation安装Linux虚拟机
- Numpy学习