教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 python 重载内置函数吗

python 重载内置函数吗

发布时间:2021-05-19   编辑:jiaochengji.com
教程集为您提供python 重载内置函数吗等资源,欢迎您收藏本站,我们将为您提供最新的python 重载内置函数吗资源

python中是不支持函数重载的,但在python3中提供了这么一个装饰器functools.singledispatch,它叫做单分派泛函数,可以通过它来完成python中函数的重载,让同一个函数支持不同的函数类型,它提供的目的也正是为了解决函数重载的问题。

相关推荐:《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

您可能感兴趣的文章:
python 重载内置函数吗
没学过编程可以自学python吗
PyCharm社区版够用吗
go语言和python哪个难
python是否支持重载
Python中sep是函数吗?该怎么使用?
只学python能找工作吗
python文件操作需要导入模块吗
python找工作难吗
python中divmod是什么意思

[关闭]
~ ~