My question relates to how to define an optional parameter which can be of different types (polymorphic).
I was trying to define a wrapper around 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 shows that I can generally do something like:
def info(object, spacing=10, collapse=1):
But in this context, the initializer can be many different types with different default values. For example, it can be 0 for addition (as the reduce function) and "" (empty string) for string concatenation. How should I define this parameter?
if type(var) is str: do_stuff.It's not uncommon (but generally ill-advised) to expect and handle different types of argument in Python@zamuz Alright. But this is standard python library function (reduce).functools.reducehasreduce(function, sequence[, initial]) -> value, with[, initial]being "placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty." The type forinitialdoes not modify/control the behavior ofreducebased on its own contents/type.