Skip to content

Instantly share code, notes, and snippets.

@mtth
Last active August 29, 2015 14:10
Show Gist options
  • Save mtth/587e4a0965276059ea53 to your computer and use it in GitHub Desktop.
Save mtth/587e4a0965276059ea53 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
"""Python numbers magic!
https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/
Adapted for python 2.7.
"""
import ctypes
class IntStruct(ctypes.Structure):
"""(Part of) Python's internal integer representation."""
_fields_ = [
('ob_refcn', ctypes.c_long),
('ob_type', ctypes.c_void_p),
('ob_digit', ctypes.c_long),
]
def magic(a, b):
"""Puts `b` in `a`!"""
a_int = IntStruct.from_address(id(a))
a_int.ob_digit = b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment