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


Groups > comp.lang.python > #24928

Re: 2 + 2 = 5

Date 2012-07-05 16:20 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: 2 + 2 = 5
References <7x7guj2u8a.fsf@ruckus.brouhaha.com> <4FF5A5EE.9050308@shopzeus.com>
Newsgroups comp.lang.python
Message-ID <mailman.1832.1341501634.4697.python-list@python.org> (permalink)

Show all headers | View raw


On 05/07/2012 15:34, Laszlo Nagy wrote:
> On 2012-07-04 21:37, Paul Rubin wrote:
>> 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.
>
>   >>> five.contents[five.contents[:].index(5)] = 4
>   >>> 5
> 4
>   >>> 5 is 4
> True
>
> But this I don't understand:
>
>   >>> 5+0
> 4

5 is interned as 4, 0 is interned as 0, 4+0 is calculated as 4 and then
interned as 4.

>   >>> 5+1
> 4

5 is interned as 4, 1 is interned as 1, 4+1 is calculated as 5 and then
interned as 4.

>   >>> 5+2
> 6
>
5 is interned as 4, 2 is interned as 2, 4+2 is calculated as 6 and then
interned and 6.

Simple really! :-)

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


Thread

Re: 2 + 2 = 5 MRAB <python@mrabarnett.plus.com> - 2012-07-05 16:20 +0100

csiph-web