in python 2 you could change the values of small integer literals (range from -5 to 256), they're cached in memory as
PyIntObjects for some reason
Python:
import ctypes
# some fucking magic offset i dunno
offset = ctypes.sizeof(ctypes.c_size_t) + ctypes.sizeof(ctypes.c_voidp)
int_entry = ctypes.c_long.from_address(id(69) + offset)
int_entry.value = 420
print 69 # 420
print 35 + 34 # 420
Of all the languages that allow you to do something like this, this is by far the dumbest. Fortran uses call by value-return, where parameters are moved into a subroutine's argument variables (the addresses of which are known at compile-time, because classic Fortran didn't allow recursion, so all variables were like C's
static locals) and then copied back out upon return, and the IBM 704 and its successors didn't allow for immediate operands, which meant that all constants in an expression had to be loaded in from a constant table, so FORTRAN I would let you pass in an entry from the constant table as an argument and would then mutate the constant table by copying the result value back out. (They fixed this in FORTRAN II, where non-variable arguments got copied in, but didn't get copied back out upon return. And if you're wondering why they didn't just put the constant table in the
RODATA segment, it's because the hardware had no memory protection, so you couldn't have read-only data.)
Code:
CALL roody-poo(69)
WRITE (6, 10) 69
10 FORMAT (I5 /)
STOP
END
SUBROUTINE roody-poo(N)
N = 420
RETURN
END
In Forth, a procedure (which the language calls a "word") name can be any sequence of non-whitespace characters. Historically, it was implemented using threaded code and the code generated to push an integer literal onto the parameter stack would be a pointer to the word that does the pushing (usually called
LIT or
(LIT)), followed by the literal itself, and implementations would define words whose identifiers were numeric literals and whose only action was to push that value onto the stack, since each reference to the literal would then be half the size (just a pointer to the word). While this usually consisted of
Code:
-1 CONSTANT -1 0 CONSTANT 0 1 CONSTANT 1 2 CONSTANT 2
You could also define abominations like
Code:
420 CONSTANT 69
69 . CR ( call word "69" and print its return value of 420 )
069 . CR ( push literal 69 and print it )
You can at least understand why these languages allowed you to change the value of an integer literal, since one was the very first compiler ever and the implementors didn't know then what we know now, and the other is just an extremely generalized notion of "identifier" that trusts the programmer to be a mature adult who won't do anything stupid (and it lets you use the standard ASCII penis
8==D as an identifier, so it's perfect just as it is). In Python's case, on the other hand, it's just a really bad implementation that lets you mutate a constant table because they were too lazy to implement an immutable structure and substituted a mutable structure because they already had one present in their standard library.