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


Groups > comp.lang.python > #24894

Re: 2 + 2 = 5

From Terry Reedy <tjreedy@udel.edu>
Subject Re: 2 + 2 = 5
Date 2012-07-04 23:38 -0400
References <7x7guj2u8a.fsf@ruckus.brouhaha.com> <op.wgxvkeuvg7njmm@michael-think>
Newsgroups comp.lang.python
Message-ID <mailman.1808.1341459524.4697.python-list@python.org> (permalink)

Show all headers | View raw


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


Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web