Language design

Chris Angelico rosuav at gmail.com
Fri Sep 13 03:39:27 EDT 2013


On Fri, Sep 13, 2013 at 3:08 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> For example, take intersection of two sets s and t. It is a basic
> principle of set intersection that s&t == t&s.

Note that, while this is true, the two are not actually identical:

>>> set1 = {0,1,2}
>>> set2 = {0.0,1.0,3.0}
>>> set1&set2
{0.0, 1.0}
>>> set2&set1
{0, 1}
>>> (set1&set2) == (set2&set1)
True

I'd actually posit that Python has this particular one backward (I'd
be more inclined to keep the left operand's value), but it's
completely insignificant to most usage. But in any case, there's
already the possibility that a set union can be forced to make a
choice between two equal objects, so we're already a bit beyond the
purity of mathematics. Python could have implemented dicts much more
like "sets with values", with set semantics maintained throughout, but
it'd require some oddities:

>>> {1:"asdf"} == {1:"asdf"}
True
>>> {1:"asdf"} == {1:"qwer"}
False

"Sets with values" semantics would demand that these both be True,
which is grossly unintuitive.

So while it may be true in pure mathematics that a set is-a dict (or a
dict is-a set), it's bound to create at least as many gotchas as it
solves.

ChrisA



More information about the Python-list mailing list