Using MethodType to convert builtin function to instancemethod.
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
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 |
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
# Compile this using: | |
# python setup.py build_ext --inplace | |
def incx(self): | |
"""Increment x.""" | |
self.x += 1 |
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
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 |
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
# 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