Created
May 11, 2025 14:50
-
-
Save MaddyGuthridge/c535fe55b1cdbc04c403a1cfe6822548 to your computer and use it in GitHub Desktop.
Arrow operator in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ctypes | |
| class ArrowInt(int): | |
| def __neg__(self) -> int: | |
| prev = int(self) | |
| self_addr = id(self) | |
| int_addr = self_addr + ctypes.sizeof(ctypes.c_size_t) + 2 * ctypes.sizeof(ctypes.c_void_p) | |
| ctypes.c_int.from_address(int_addr).value = self - 1 | |
| # Return previous value so it loops a sensible number of times | |
| return prev | |
| x = ArrowInt(5) | |
| while 0 <- x: | |
| print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment