D
Diez B. Roggisch
vdrab said:Given this though, what other such beauties are lurking in the
interpreter, under the name of 'implementation accidents'? One of the
things that drew me to python is the claimed consistency and
orthogonality of both language and implementation, not sacrificing
clarity for performance, minimizing ad-hoc design hacks and weird
gotcha's, etc...
In fact, I think my code contains things like "if len(arg) is 0:" and
so on, and I feel I should be able to do so given the way python treats
(claims to treat?) constant objects, even if I don't care whether the
values actually represent the same object.
Python doesn't claim that 0 is 0 == True. You are abusing the "is" operator.
The only (or at least 99%) occasions I use "is" are
if foo is None:
...
as None is guaranteed to be a singleton object.
The thing you observe as accident is that sometimes "0 is 0" is true just
because of an optimization of number objects allocation. Such things happen
in the "real" world - other examples are string-interning in e.g. the JVM
(and I bet they have a similar scheme to boxed number object allocation as
python has).
Diez