0

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?

9
  • It's not uncommon (but generally ill-advised) to expect and handle different types of argument in Python, e.g. if type(var) is str: do_stuff. Commented Feb 19, 2016 at 22:40
  • 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). Commented Feb 19, 2016 at 22:41
  • would you please make clear what you are trying to achieve? The parameter type is determined at runtime (i.e. you never define the parameter type) . It would be quite helpful to see the actual result vs the expected one Commented Feb 19, 2016 at 22:42
  • @tinlyx: The pydoc of functools.reduce has reduce(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 for initial does not modify/control the behavior of reduce based on its own contents/type. Commented Feb 19, 2016 at 22:44
  • @ProfHase85 As in the OP, I am trying to provide a wrapper of the reduce function. Commented Feb 19, 2016 at 22:44

1 Answer 1

1

So what about simply?

def reduce_wrapper(custom_param, function, iterable, *args):
    # do stuff
    reduce(function, iterable, *args)
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I tried this, e.g.: def reduce_(f,xs,init=None): return functools.reduce(f,xs,init) then reduce_ ((lambda x,y:x+y), [1,2,3]). But I got: TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
sorry for that. There seems to be a mismatch in the documentation. It seems like initializer is a simple positional argument
Thanks. This works well. Still curious though, about how a positional argument solved the problem in question inside functools.reduce.
the reduce function seems not to be a python function but some strange compiled product. So I did not find the way to get the signature. But if you start playing around with the reduce function, you will see that there is no keyword arguments taken by it (eg you cannot enter something like reduce(...., initializer=5) . It means it is a positional argument with no defined value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.