[Python-ideas] Support multiplication for sets
Stephen J. Turnbull
stephen at xemacs.org
Sat Oct 8 04:58:38 CEST 2011
Terry Reedy writes:
> If the elements of A*B are sequences, then A*B is also not commutative.
> But it would be if the elements were sets instead of pairs.
But that's not a Cartesian product. By definition in a Cartesian
product order of element components matters. I don't think I've ever
seen a set product like that, and have trouble imagining applications
for it unmodified (typically when squaring a set the diagonal would
cause problems).
> In math, *...* is a ternary operator with 3 args, like if...else in
> Python or ;...: in C, but it generalizes to an n-ary operator.
A better analogy is to the comma or string concatenation. I don't
know if that would lead to an associative implementation, though.
> In most situations, one really needs an iterator that produces the
> pairs one at a time rather than a complete collection.
I don't see why you couldn't have an operator on two iterables that
produces an iterator. But of course comprehension notation is hard to
beat for that.
> And when one does want a complete collection, one might often want
> it ordered as a list rather than unordered as a set.
I don't understand this. Sets are unordered; any order you impose on
the product would be arbitrary. So iterate the product as a set, what
else might be (commonly) wanted?
More information about the Python-ideas
mailing list