Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24894 > unrolled thread

Re: 2 + 2 = 5

Started byTerry Reedy <tjreedy@udel.edu>
First post2012-07-04 23:38 -0400
Last post2012-07-05 16:16 +0000
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: 2 + 2 = 5 Terry Reedy <tjreedy@udel.edu> - 2012-07-04 23:38 -0400
    Re: 2 + 2 = 5 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-05 05:32 +0000
      Re: 2 + 2 = 5 Hans Mulder <hansmu@xs4all.nl> - 2012-07-05 15:57 +0200
        Re: 2 + 2 = 5 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-05 16:16 +0000

#24894 — Re: 2 + 2 = 5

FromTerry Reedy <tjreedy@udel.edu>
Date2012-07-04 23:38 -0400
SubjectRe: 2 + 2 = 5
Message-ID<mailman.1808.1341459524.4697.python-list@python.org>
On 7/4/2012 4:37 PM, Michael Ross wrote:
> Am 04.07.2012, 21:37 Uhr, schrieb Paul Rubin <phr-2012@nightsong.com>:
>
>> I just came across this (https://gist.github.com/1208215):
>>
>>     import sys
>>     import ctypes
>>     pyint_p = ctypes.POINTER(ctypes.c_byte*sys.getsizeof(5))
>>     five = ctypes.cast(id(5), pyint_p)
>>     print(2 + 2 == 5) # False
>>     five.contents[five.contents[:].index(5)] = 4
>>     print(2 + 2 == 5) # True (must be sufficiently large values of 2
>> there...)
>>
>> Heh.  The author is apparently anonymous, I guess for good reason.
>
>
> Neat.
>
> Playing with it, i'm wondering:
>
>
> This:
>
>      import sys
>      import ctypes
>      pyint_p = ctypes.POINTER(ctypes.c_byte*sys.getsizeof(5))
>      five = ctypes.cast(id(5), pyint_p)
>      five.contents[five.contents[:].index(5)] = 4
>
>      print ( 2 + 2 == 5 )
>      print 5
>      print 5 - 2
>
> put into a script and run prints:
>
>      True
>      4
>      3

The compile-time optimizer computed the contant 5-2 in C.

> while entered at the python prompt it prints:
>
>      True
>      4
>      2

It must not run for interactive input with the undisclosed version you 
are running.

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.

-- 
Terry Jan Reedy


[toc] | [next] | [standalone]


#24898

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-07-05 05:32 +0000
Message-ID<4ff52700$0$30000$c3e8da3$5496439d@news.astraweb.com>
In reply to#24894
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 :)


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#24914

FromHans Mulder <hansmu@xs4all.nl>
Date2012-07-05 15:57 +0200
Message-ID<4ff59d61$0$6880$e4fe514c@news2.news.xs4all.nl>
In reply to#24898
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.


Confused,

-- HansM

[toc] | [prev] | [next] | [standalone]


#24934

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-07-05 16:16 +0000
Message-ID<4ff5bdd1$0$29988$c3e8da3$5496439d@news.astraweb.com>
In reply to#24914
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web