[Python-3000] bytes and dicts (was: PEP 3137: Immutable Bytes and Mutable Buffer)
Guido van Rossum
guido at python.org
Fri Sep 28 20:40:40 CEST 2007
On 9/28/07, Adam Hupp <adam at hupp.org> wrote:
> On 9/28/07, Guido van Rossum <guido at python.org> wrote:
> > Well, maybe this is a good enough argument to give up. If the best we
> > can say is that having a bytes and a str as keys *may* cause a
> > TypeError on lookups, I'm not sure it is worth it to try to raise the
> > probability that it'll actually be raised...
> Would it make sense to have dict ignore TypeError on lookups?
> Alternatively, the byte/str comparison could throw a specific subclass
> of TypeError that dict ignored e.g. IncompatibleComparisonError.
Well, if we wanted "x" and b"x" to compare unequal instead of raising
an exception, we could just define it that way (it was that way until
just before 3.0a1). But we're explicitly defining it to raise a
TypeError so as to catch buggy code. I think trying to fix dict lookup
so that it, and only it, treats this as unequal, would be adding too
We could choose to kill the TypeError altogether. If we keep it, we
should consistently let it raise TypeError everywhere.
The question is whether it's worth the effort to raise TypeError when
the *potential* exists that a certain hash sequence *could* raise this
TypeError. I'm less and less convinced -- after all, we're making the
exception only for bytes/str, not for other types that might raise
TypeError upon comparison.
So, I think that after all this was a bad idea. Sorry.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000