[Python-Dev] if key in dict?
Fred L. Drake, Jr.
fdrake@acm.org
Thu, 13 Apr 2000 09:46:44 -0400 (EDT)
Skip Montanaro writes:
> Also, for many situations, "if value in dict" will be extraordinarily
> inefficient. In "in" semantics are added to dicts, a corollary move will be
> to extend this functionality to other non-dict mappings (e.g., file-based
> mapping objects like gdbm). Implementing "in" for them would be
> excruciatingly slow if the LHS was "value". To not break the rule of least
> astonishment when people push large dicts to disk, the only feasible
> implementation is "if key in dict".
Skip,
Performance issues aside, I can see very valid reasons for the x in
"x in dict" to be either the key or (key, value) pair. For this
reason, I've come to consider "x in dict" a mis-feature, though I once
pushed for it as well. It may be easy to explain that x is just the
key, but it's not clearly the only reasonably desirable semantic.
-Fred
--
Fred L. Drake, Jr. <fdrake at acm.org>
Corporation for National Research Initiatives