Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > 2 + 2 = 5

Reply
Thread Tools

2 + 2 = 5

 
 
Paul Rubin
Guest
Posts: n/a
 
      07-04-2012
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.
 
Reply With Quote
 
 
 
 
Stefan Behnel
Guest
Posts: n/a
 
      07-04-2012
Paul Rubin, 04.07.2012 21:37:
> 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.


That's not portable, though.

Stefan

 
Reply With Quote
 
 
 
 
Michael Ross
Guest
Posts: n/a
 
      07-04-2012
Am 04.07.2012, 21:37 Uhr, schrieb Paul Rubin <phr->:

> 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
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      07-04-2012
On 04/07/2012 20: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.
>


The author got confused trying to switch from imperial to metric numbers
or vice versa?

--
Cheers.

Mark Lawrence.



 
Reply With Quote
 
Thomas Jollans
Guest
Posts: n/a
 
      07-04-2012
On 07/04/2012 09:37 PM, 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.
>


I'm reminded of the swap(a,b) function I wrote a couple of years back.

http://blog.jollybox.de/archives/62-python-swap
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      07-05-2012
On Jul 5, 5:37*am, Paul Rubin <phr-2...@nightsong.com> wrote:
> * * print(2 + 2 == 5) # True (must be sufficiently large values of 2 there...)


I believe CCP use a variant of this approach in EvE with Stackless
Python.
 
Reply With Quote
 
Evan Driscoll
Guest
Posts: n/a
 
      07-05-2012
On 7/4/2012 14: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.


Probably just nostalgic for old Fortran, which, supposedly, allowed you
to change the values of literals by passing them to a function by
reference and then modifying the value.

Evan



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJP9OJBAAoJEAOzoR8eZTzgPyQH/RYRdhNWV7XjjAg4SgNjt3wj
a0KSrKHg8TPKb2Sbj6M+t09UzE6dvwQMpUuO6oviue4pUgrPa9 E0OK7TuWYi/3AA
J5GoC8xoWuy1t9+7Zu/fBC5ikuwPmDNOLhP0WgPCCdS35EZbNPvVg7FpvsidXeYZ
aFUFYi5VvJFmCPIGHFLqsomljMkavOde2ZtuPXSPgp4cBkb0sR xsMebN9xPjRWO9
5Sb6hchtwYZznJpc19WWCMSnIY+Ug30jsdP09dlr8SQf+PalOy BJbM9BoSaQ9NCG
ctFB/wDO6zamzr/Ba4vEA+9I6b4hsnjFe6l+1hlUgKBTxfbRqYoA4x6DGoz2Nb0=
=MvhR
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      07-05-2012
On 7/4/2012 4:37 PM, Michael Ross wrote:
> Am 04.07.2012, 21:37 Uhr, schrieb Paul Rubin <phr->:
>
>> 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



 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-05-2012
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
 
Reply With Quote
 
Hans Mulder
Guest
Posts: n/a
 
      07-05-2012
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments