python中functools.singledispatch的使用
来源:互联网 发布:微积分 清华 知乎 编辑:程序博客网 时间:2024/06/03 08:37
在python3中提供了functools.singledispatch这个装饰器,但是这个装饰器是做什么呢?其实它是单分派泛函数,看到这个名词,一脸的懵逼,这是什么鬼,然而大家都知道c++中的函数是可以重载的,那么它的作用就和c++中函数的重载类似。下面看个代码就知道它的作用了
from functools import singledispatch@singledispatchdef show(obj): print (obj, type(obj), "obj")@show.register(str)def _(text): print (text, type(text), "str")@show.register(int)def _(n): print (n, type(n), "int")show(1)show("xx")show([1])
结果
1 <class 'int'> intxx <class 'str'> str[1] <class 'list'> obj
结论:通过上述代码的结果可以知道,为show函数传递不同的类型参数,就表现不同的行为,这c++的函数重载很类似。
阅读全文
0 0
- python中functools.singledispatch的使用
- python--functools的使用
- python中functools.wraps的作用
- Python的functools模块
- Python的functools模块
- Python的functools模块
- Python的functools模块
- Python的functools模块
- Python的functools模块
- Python 中实现装饰器时使用 @functools.wraps 的理由
- functools.partial 的使用
- windows python tornado的安装no module named singledispatch错误
- python的functools进阶模块
- Python的functools模块详解
- python functools
- python functools
- python functools
- 飘逸的python - 偏函数functools.partial
- 中国主要地图投影
- Codeup_100000575_问题1814_剩下的树
- xml详解
- LeetCode 198 House Robber(基础DP)
- 【数据结构】三遍读书法的成果-思维导图
- python中functools.singledispatch的使用
- 全文检索技术 lucene(一) 一个简单的Demo
- JZOJ4446. 【HNOI2016模拟4.14】B
- SELECT 基本语法
- Comparable与Comparator的区别
- 安卓入门【linearlayout线性布局,Tablelayput表格布局】
- Jquery选择器全总结
- 搭建iDempiere4.1开发环境
- SecureCRT常用命令