如何定义多态python可选参数?

时间:2016-02-19 22:33:46

标签: python python-3.x optional-parameters

我的问题涉及如何定义可选参数,该参数可以是不同类型(多态)。

我试图在python 3.x中定义functools.reduce周围的包装器,并注意到有一个可选参数create view RowNumberedView as select ISNULL(ROW_NUMBER() OVER (ORDER BY <column>), 0) AS RN , * from your_existing_view 。我试图定义相同的可选参数,但不知道如何。 Search around表明我通常可以执行以下操作:

,[initializer]

但是在这种情况下,def info(object, spacing=10, collapse=1): 可以是许多不同的类型,具有不同的默认值。例如,对于字符串连接,可以initializer添加(作为reduce函数)和0(空字符串)。我该如何定义这个参数?

1 个答案:

答案 0 :(得分:1)

那么简单呢?

def reduce_wrapper(custom_param, function, iterable, *args):
    # do stuff
    reduce(function, iterable, *args)