def __init__(self):

Chris Kaynor ckaynor at zindagigames.com
Tue Apr 26 12:59:39 EDT 2016


On Tue, Apr 26, 2016 at 9:32 AM, Steven D'Aprano <steve at pearwood.info>
wrote:

> Subclassing immutable built-ins is the most obvious and simple (and
> probably
> common) way to get an immutable class. Actually immutable, short of doing
> wicked things with ctypes.
>

By wicked things with ctypes, do you mean something like this? By no means
do I suggest this actually be used by anybody for any reason.

Tested with '2.7.10 (default, Jul 14 2015, 19:46:27) \n[GCC 4.2.1
Compatible Apple LLVM 6.0 (clang-600.0.39)]'

import ctypes
def changeTuple(tuple, index, newValue):
    obj = ctypes.cast(id(tuple), ctypes.POINTER(ctypes.c_long))
    obj[3+index] = id(newValue)

>>> a = ('a','b','c')
>>> changeTuple(a, 0, 1)
>>> a
(1, 'b', 'c')
>>> changeTuple(a, 1, 3)
>>> a
(1, 3, 'c')

Chris



More information about the Python-list mailing list