Skip to content

Instantly share code, notes, and snippets.

@bfroehle bfroehle/bad.py
Created Jan 27, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.