python3中实现函数的重载
2017-07-15 21:29
295 查看
python中是不支持函数重载的,但在python3中提供了这么一个装饰器functools.singledispatch,它叫做单分派泛函数,可以通过它来完成python中函数的重载,让同一个函数支持不同的函数类型,它提供的目的也正是为了解决函数重载的问题。
看下面的例子,应该知道怎么去使用它完成函数的重载。
结果:
看下面的例子,应该知道怎么去使用它完成函数的重载。
from functools import singledispatch @singledispatch def 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'> int xx <class 'str'> str [1] <class 'list'> obj
相关文章推荐
- Javascript实现函数重载
- python里使用正则表达式的search()函数实现指定位置搜索
- Python可变参数函数的实现
- python中的__init__函数重载与覆盖问题
- PHP实现函数重载
- 【C++实现python字符串函数库】一:分割函数:split、rsplit
- python 实现 Peceptron Learning Algorithm ( 一) 几个函数的记录
- python用装饰器实现缓存函数执行结果
- Python实现接受任意个数参数的函数方法
- Python不使用函数,实现用户登录控制
- python通过ntohl和htonl等函数实现主机字节序和网络字节序相互转换
- 在 lua 中实现函数的重载
- python 实现多变量核密度评估函数
- kNN算法实现,及python库和函数的使用方法回顾
- Python使用函数默认值实现函数静态变量的方法
- 第22课 Python字符串分割函数设计与实现
- 用C语言来实现的类似C++函数的重载特性-----void*指针闲谈
- python 禁止函数修改列表的实现方法
- paip.编程语言方法重载实现的原理及python,php,js中实现方法重载