Python String Immutability Broken!

Peter Otten __peter__ at web.de
Mon Aug 25 09:45:49 EDT 2008


Steven D'Aprano wrote:

> On Mon, 25 Aug 2008 03:43:01 -0700, Ken Seehart wrote:
> 
>> You can also use ctypes to globally change the value of integers less
>> than 101.   Personally, I don't particularly like the number 14.  I
>> changed it to 9 and I am much happier now.
> 
> Okay, you've got me curious. How do you do that, and why only up to 101?

Up to 256 in current Python. Small integers are shared to save memory.
After a quick look into the ctypes tutorial and the python source I came up
with

# 64-bit linux

>>> from ctypes import *
>>> libc = cdll.LoadLibrary("libc.so.6")
>>> libc.memset(id(14)+16, 0, 8)
7742336
>>> 14
0
>>> 14 == 0
True

Peter



More information about the Python-list mailing list