[Python-ideas] Support multiplication for sets

Nick Coghlan ncoghlan at gmail.com
Fri Oct 7 23:35:18 CEST 2011


On Fri, Oct 7, 2011 at 11:28 AM, Haoyi Li <haoyi.sg at gmail.com> wrote:
> I don't think having itertools.product is a good reason for not
> overloading the operator. The same argument could be said against
> having listA + listB or listA * 10. After all, those can all be done
> with list comprehensions and itertools aswell.
>
> itertools.product(a, b) or a list comprehension work fine for 2 sets,
> but if you try doing that for any significant number (which i presume
> the OP is), maybe 5 set operations in one expression, it quickly
> becomes completely unreadable:
>
> itertools.product(itertools.product(seta, setb.union(setc),
> setd.difference(sete))
>
> vs
>
> seta * (setb | setc) * (setd & sete)

I expect what you actually intended here was:

cp = product(seta, setb|setc, setd&sete)

If you actually did want two distinct cartesian products, then the two
line version is significantly easier to read:

cp_a = product(setb|setc, setd&sete)
cp_b = product(seta, cp_a)

The ambiguity of chained multiplication is more than enough to kill
the idea (although it was definitely worth asking the question).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list