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

Nathaniel Gray n8gray at caltech.edu.is.my.email.address
Thu Feb 1 03:43:07 EST 2001


Thomas Wouters wrote:
> 
> My main beef with the 'x in dict' idea is something Nathaniel didn't
> raise, though: we all seem to think 'x in dict' is naturally 'key in dict'
> -- but why ? 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 ? What,
> exactly, is the target group for Python ? :)

I think this may be what truly underlies my discomfort with 'in dict'.  At 
some fundamental level _values_ are what we store in dictionaries, not 
keys.  (Unless we're kludging together a Set :^) The only reason we have 
the keys around is to get to the values.  So when I talk about something 
being "in" a dictionary, it's the value.  I can understand, though, that 
from a practical standpoint the keys may be accessed and iterated over more 
frequently.

> > [Timbot]
> > We still don't have anything else for 2.1; only "a_key in dict" in a
> > *boolean* context has been implemented.  And, yes, we've already got
> > dict.has_key(a_key) for that.  And, yes, Guido just *approved* of adding
> > "for a_key in dict" too, although implementing that is a puzzle given
> > the current iteration protocol's use of __getitem__ (it doesn't jibe
> > with what dict.__getitem__ does), and "what to do" about programs that
> > go mutating the dict during iteration.

Sorry if I munged up the for/if distinction.  The idea's the same:  To me, 
'in dict' doesn't logically imply 'in dict.keys()'.

I'm reassured that I'm right by the fact that, until recently Guido agreed 
with me.  Has he ever used the time machine to go _forward_ in time?

In-a-battle-between-old-Guido-and-new-Guido-who-would-win?-ly-yrs,

-n8

-- 
_.~'^`~._.~'^`~._.~'^`~._.~'^`~._.~'^`~._
             Nathaniel Gray
   California Institute of Technology
     Computation and Neural Systems
     n8gray <at> caltech <dot> edu
_.~'^`~._.~'^`~._.~'^`~._.~'^`~._.~'^`~._




More information about the Python-list mailing list