[Tutor] Tuple - Immutable ?
Steven D'Aprano
steve at pearwood.info
Thu Mar 8 14:31:15 CET 2012
col speed wrote:
> On 8 March 2012 18:51, Steven D'Aprano <steve at pearwood.info> wrote:
>> This makes sense, and is easy to understand. Now for the real boggle:
>>
>> py> a = 9912346; b = 9912346
>> py> a is b
>> True
>>
>> Can you guess what is going on here?
>>
>> (Answer will follow later.)
> Because it's on the same line and there are 2 variables with the same
> int, it gets cached???
Almost correct, but well done.
Yes, the trick is that they are on the same line.
But you're *almost* correct, because the value doesn't actually gets cached.
It only gets re-used in the context of that one line. If you then follow with
a second line using the same 9912346, it gets a different ID.
All of these details are implementation tricks. They are subject to change
without notice, and the exact rules are not documented anywhere (except
perhaps in the source code) so there is no language promise that they will
apply. But they are interesting little tricks nevertheless.
--
Steven
More information about the Tutor
mailing list