Help with sets

Steve Howell showell30 at yahoo.com
Tue Oct 12 01:00:11 EDT 2010


On Oct 11, 5:11 pm, Gregory Ewing <greg.ew... at canterbury.ac.nz> wrote:
> Robert Kern wrote:
> > The reasoning for this decision is spelled out in the PEP introducing
> > the iterator feature:
>
> >  http://www.python.org/dev/peps/pep-0234/
>
> There's also the question of whether 'if x in dict' should
> compare keys only or both keys and values. This was also
> hotly debated back when dicts were given an 'in' operator
> (before that you had to use dict.haskey(x)). Can't remember
> all the arguments, but it was definitely thought about.
>
> --
> Greg

The PEP seems to refer to such discussion, although no there is no
specific footnote to the mailing list archives.

    - Regarding "if key in dict": there is no doubt that the
      dict.has_key(x) interpretation of "x in dict" is by far the
      most useful interpretation, probably the only useful one.  There
      has been resistance against this because "x in list" checks
      whether x is present among the values, while the proposal makes
      "x in dict" check whether x is present among the keys.  Given
      that the symmetry between lists and dictionaries is very weak,
      this argument does not have much weight.

Symmetry is always a tricky balance in programming languages.  Python
discounts symmetry considerations with respect to lists and
dictionaries when it comes to "in" (see above), while it goes for a
certain symmetry between "if" and "for" with respect to the "in"
keyword and dictionaries (see below):

      While this is true, I (Guido) find the correspondence between
"for x in
      dict" and "if x in dict" too compelling to break, and there's
not
      much overhead in having to write dict[x] to explicitly get the
      value.

There is no major inconsistency here; "symmetry" is subjective and
needs to be applied with a heavy dose of pragmatism.



More information about the Python-list mailing list