"in" for dicts (was: Python 2.1 function attributes)

Thomas Wouters thomas at xs4all.net
Mon Jan 29 09:15:31 EST 2001


On Mon, Jan 29, 2001 at 07:08:32AM -0500, Tim Peters wrote:
> [Thomas Wouters]
> > Two of my colleagues (out of two interrogated) didn't quite get
> > it [x in dict, for x in dict]. It's not that they didn't understand
> > the idea, but it wasn't quite what they expected. They aren't quite
> > feeble :) But it's also true that their expectations were different
> > in the 'for' case.

> Don't be such a tease:  what *did* they expect?  I'm guessing they expected
> to iterate over (key, value) pairs in the "for" case -- but only because
> that's what you suggested on Python-Dev <wink>.

Well, one expected 'if value in dict' but 'for key in dict', but said he
would prefer it if it was 'for key,value in dict' -- eventhough that's not
what he expected.

The other *wanted* 'if value in dict', but refused to say what he *expected*
it to do. Similar for 'for value in dict', though after some prodding of
mine (that is, I suggested some options) admitted 'for key in dict' and 'for
key,value in dict' both were useful, too. He even suggested exactly what I
suggested, 'for key in dict.keys', though with less sarcasm (i.e. none.)

> > Is it because we all know you can't *get* a value from a dict without
> > extracting them all, and all values could be the same ? How obvious is
> > that to people without a technical idea of dictionaries ?

> Well, I suspect the idea that you *could* "look up" by value is something
> only a tech-head would imagine.  Nobody searches phone books or Webster's
> "backwards" in real life.  Even tech-heads don't seem to have much use for
> it, to judge from Ping's quantification of "[for] x in dict.values()"
> constructs in the std library:

>     http://mail.python.org/pipermail/python-dev/2001-January/012106.html

> I didn't think that was surprising.

And this still makes me say 'pfiffle'. That construct A is used (much more)
often than construct B doesn't necessarily mean that syntax X should be tied
to construct A (or B or C, for that matter.) Are we talking "what do you
expect it to mean" or "what do you wish it meant" (or "what idiom do you
want syntactic support for") ?

> [Guido] approved of "Ping's proposal", and at the top of the msg in which
> he did that:
> 
>     http://mail.python.org/pipermail/python-dev/2001-January/012110.html

The first agreement from Guido in this thread is here:
http://mail.python.org/pipermail/python-dev/2001-January/012103.html

and seems to imply he only agreed to the 'if' part. The message you supply
came later, and isn't clear to what he means. I suspect, too, that he more
or less agreed to both, knowing fully well it would be hackish, if not
impossible, to implement 'for x in dict' before 2.1b1, giving him ample time
to rethink the 'for' part :-)

> That is, in

>     http://mail.python.org/pipermail/python-dev/2001-January/012116.html

> you explained why your patch *didn't* implement the "for" part.  Seems an
> odd digression if you didn't think Guido was asking for that too <wink>.

Hmm. I think I meant it more to the rest of the public, and to make clear
that it was *not* implemented, than as an acknowledgement that Guido had
wanted it implemented. I'm not a professional Guido channeler, and I
definately wasn't trying to do that, there :)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list