2 + 2 = 5
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Thu Jul 5 12:16:17 EDT 2012
On Thu, 05 Jul 2012 15:57:53 +0200, Hans Mulder wrote:
> On 5/07/12 07:32:48, Steven D'Aprano wrote:
>> On Wed, 04 Jul 2012 23:38:17 -0400, Terry Reedy wrote:
>>
>>> If I run the script in 3.3 Idle, I get the same output you got. If I
>>> then enter '5-2' interactively, I still get 3. Maybe the constant
>>> folder is always on now.
>>
>> Yes, I believe constant folding is always on, since Python 2.4 if I
>> remember correctly. Somebody who cares more than me can possibly check
>> the "What's New" documents :)
>
> It's not a difference between 2.4 and 3.3; the difference is between
> Idle and the command-line version of the interactive interpreter.
>
> If I type the same code into Idle and the interactive interpreter (both
> using 3.3alpha1), I get 3 in Idle and 2 in Terminal.
>
> I don't quite understand why this difference exists.
This difference exists because you are mucking about with implementation
details of Python, changing things which are not guaranteed by the
language, in ways that you are not supposed to change them. Since
changing the value of the int 2 into that of 3 is not supported, you can
hardly expect consistent behaviour. You're lucky that you don't get a
segfault. (In fact, if you keep playing around with it, you likely will
get a segfault.)
Idle does many things differently to the basic Python interpreter. This
should not surprise you. You should be surprised if it *does* work the
same in Idle and the basic Python interpreter.
--
Steven
More information about the Python-list
mailing list