Last active
August 29, 2015 14:10
-
-
Save mtth/587e4a0965276059ea53 to your computer and use it in GitHub Desktop.
This file contains 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
#!/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