If I want to keep things functional, and do not want to use *
in the middle, then what is the equivalent substitute function?
for example,
import operator as op
print(op.eq(*map(str.upper, ['a', 'A'])))
how do I avoid using *
here?
I created a function, like,
def unpack(args):
return *args
but it gives syntax error, print(*args)
works but return
fails
CodePudding user response:
functools.reduce
can be used to apply a function of two arguments to an iterable cumulatively, which would work for your example case
import operator as op
from functools import reduce
print(reduce(op.eq, map(str.upper, ['a', 'A'])))
CodePudding user response:
You could use reduce
to check if all of the mapped elements are equal to each other:
from functools import reduce
print(reduce(op.eq, map(str.upper, ['a', 'A']), True))
I'm not convinced that's the right thing to do, though. If your plan is to call op.eq
with exactly two operands, no more and no less, then unpacking with *
is appropriate. Even in a functional context.