如何定义多态的python可选参数?
How to define polymorphic python optional parameter?
我的问题涉及如何定义一个可选参数,它可以是不同类型(多态)。
我试图围绕 functools.reduce
in python 3.x, and noticed that there is an optional parameter ,[initializer]
. I tried to define the same optional parameter, but don't know how. Search around 定义一个包装器表明我通常可以做类似的事情:
def info(object, spacing=10, collapse=1):
但在此上下文中,initializer
可以是具有不同默认值的许多不同类型。例如,它可以是用于加法的 0
(作为 reduce 函数)和用于字符串连接的 ""
(空字符串)。这个参数应该怎么定义?
那么简单呢?
def reduce_wrapper(custom_param, function, iterable, *args):
# do stuff
reduce(function, iterable, *args)
我的问题涉及如何定义一个可选参数,它可以是不同类型(多态)。
我试图围绕 functools.reduce
in python 3.x, and noticed that there is an optional parameter ,[initializer]
. I tried to define the same optional parameter, but don't know how. Search around 定义一个包装器表明我通常可以做类似的事情:
def info(object, spacing=10, collapse=1):
但在此上下文中,initializer
可以是具有不同默认值的许多不同类型。例如,它可以是用于加法的 0
(作为 reduce 函数)和用于字符串连接的 ""
(空字符串)。这个参数应该怎么定义?
那么简单呢?
def reduce_wrapper(custom_param, function, iterable, *args):
# do stuff
reduce(function, iterable, *args)