Tuple assignment and generators?

Diez B. Roggisch deets at nospam.web.de
Fri May 5 05:32:11 EDT 2006


vdrab wrote:

>> No.  Why should you ever care about whether two integers representing
>> values are the same object?  Your tests should be with `==`, not `is`.
> 
> 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



More information about the Python-list mailing list