2 + 2 = 5

Michael Ross gmx at ross.cx
Wed Jul 4 16:37:16 EDT 2012


Am 04.07.2012, 21:37 Uhr, schrieb Paul Rubin <phr-2012 at 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

while entered at the python prompt it prints:

	True
	4
	2

??


Regards,
Michael



More information about the Python-list mailing list