[Python] Strange behavior when using ctypes to cast/mutate the memory of a number in Python

Discussão em 'Python' iniciado por Stack, Julho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I was just reading about using the ctypes module in Python to cast interned integers to behave like different values. So I had the following code:



    ctypes.cast(id(1), ctypes.POINTER(ctypes.c_int))[6] = 2

    print(1) # Prints: 2
    print(1 + 1) # Prints: 2 Why?


    Why does the second line print 2 also if 1 is now behaving like 2? (Other numbers to the same thing.)

    Thanks!

    Continue reading...

Compartilhe esta Página