[newbie] A question about lists and strings

Chris Angelico rosuav at gmail.com
Fri Aug 10 07:00:40 EDT 2012


On Fri, Aug 10, 2012 at 8:56 PM, Dave Angel <d at davea.name> wrote:
>> On Fri, Aug 10, 2012 at 8:07 PM, Dave Angel <d at davea.name> wrote:
>>> But if you said  c=651 and d=651, you'd have two
>>> objects, and the two names would be bound to different objects, with
>>> different ids.
>> To be more accurate, you *may* have two different objects. It's
>> possible for things to be optimized (eg with small numbers, or with
>> constants compiled at the same time).
>
> You're right, of course.  But I picked the value of 650+ deliberately,
> as I believe CPython doesn't currently optimize ints over 256.

Yep. Also:

>>> a=651; b=651
>>> a is b
True
>>> a=651
>>> a is b
False

Same thing occurs (or at least, appears to) with constants in the same
module. Could potentially be a fairly hefty optimization, if you use
the same numbers all the time (bit flags, scale factors, modulo
divisors, etc, etc, etc).

Still doesn't alter your fundamental point of course.

ChrisA



More information about the Python-list mailing list