feature request: mul

Michele Simionato mis6 at pitt.edu
Wed Jun 11 18:33:33 CEST 2003


I like the new builtin 'sum', actually I missed it from my very first
Python program (I was doing hystograms at that time). Here is the question:
is there any hope to have a corrisponding 'mul' in the near future?

Of course, you could use 'mul' to compute factorials, bu my motivation is 
mainly for logical constructs.
At the present, instead of

if condition[0] or condition[1] or ....  or condition[42]:
    dosomething()

I can use (using a comment to make crystal clear what the code is doing)

if sum(condition): # multi_or
   dosomething() 

In the same sense, I would like to abbreviate
 
if condition[0] and condition[1] and ....  and condition[42]:
    dosomething()

as

if mul(condition): # multi_and
   dosomething()

I know I could use 

import operator
multi_and=lambda it: reduce(operator.mul,it)
if multi_and(condition):
   dosomething()

but frankly it is too ugly for my taste. Having
'sum' and 'mul' the use cases for 'reduce' would
drop, which I think is a good thing.

Apart for boolean algebra, I think a 'mul' with
the signature

mul(iterable, start=1)

would naturally complement 'sum'. Moreover, 
the implementation would be trivial and I don't 
see why we should not have it.

P.S. BTW, I like the concept of reduce, but
not the syntax, and that is the reason why 
I *never* use 'reduce' in my programs. A more
sensible syntax would have been something
like

apply('+',iterable)

or

apply('*',iterable)

but of course apply had another meaning.

                                                               Michele




More information about the Python-list mailing list