Skip to content

Instantly share code, notes, and snippets.

@bfroehle
Created January 27, 2012 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfroehle/1686654 to your computer and use it in GitHub Desktop.
Save bfroehle/1686654 to your computer and use it in GitHub Desktop.
Using MethodType to convert builtin function to instancemethod.
from demo import incx
class Point(object):
"""A 1-diml Point"""
def __init__(self, x):
self.x = x
incx = incx
p = Point(0)
p.incx()
print p.x
# Compile this using:
# python setup.py build_ext --inplace
def incx(self):
"""Increment x."""
self.x += 1
from demo import incx
class Point(object):
"""A 1-diml Point"""
def __init__(self, x):
self.x = x
from types import MethodType
Point.incx = MethodType(incx, None, Point)
p = Point(0)
p.incx()
print p.x
# python setup.py build_ext --inplace
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("demo", ["demo.pyx"])]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment