[Python-ideas] Support multiplication for sets
Steven D'Aprano
steve at pearwood.info
Sat Oct 8 02:23:18 CEST 2011
Jakob Bowyer wrote:
> There is that but from a math point of view the syntax a * b does make
> sence.
> Its slightly clearer and makes more sense to people from outside of a
> programming background.
I realise that the consensus is that the lack of associativity is a
fatal problem with a Cartesian product operator, but there are at least
two other issues I haven't seen.
(1) "Using * for set product makes sense to mathematicians" -- maybe so,
but those mathematicians already have to learn to use | instead of ∪
(union) and & instead of ∩ (intersection), so learning to use
itertools.product() for Cartesian product is not a major burden for them.
(2) Cartesian product is potentially very expensive. The Cartesian
product of a moderate-sized set and another moderate-sized set could
turn out to be a HUGE set.
This is not a fatal objection, since other operations in Python are
potentially expensive:
alist*10000000
but at least it looks expensive. You're multiplying by a big number, of
course you're going to require a lot of memory. But set multiplication
can very easily creep up on you:
aset*bset
will have size len(aset)*len(bset) which may be huge even if neither set
on their own is. Better to keep it as a lazy iterator rather than try to
generate a potentially huge set in one go.
--
Steven
More information about the Python-ideas
mailing list