Python 官方文档:入门教程 => 点击学习
python中是不支持函数重载的,但在python3中提供了这么一个装饰器functools.singledispatch,它叫做单分派泛函数,可以通过它来完成Python中函数的重载,让同一个函数支持不同的函数类型,它提供的目的也正是为了
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])12345678910111213141516
结果:
1 <class 'int'> int
xx <class 'str'> str
[1] <class 'list'> obj
本文来自 guoqianqian5812 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/guoqianqian5812/article/details/75194118?utm_source=copy
--结束END--
本文标题: python函数重载
本文链接: https://www.lsjlt.com/news/184446.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0