| 1 |
PYTHON setup.py build_ext --inplace |
| 2 |
PYTHON test.py |
| 3 |
|
| 4 |
######## setup.py ######## |
| 5 |
from Cython.Build import cythonize |
| 6 |
from distutils.core import setup |
| 7 |
|
| 8 |
setup( |
| 9 |
ext_modules = cythonize("*.pyx"), |
| 10 |
) |
| 11 |
|
| 12 |
######## test.py ######## |
| 13 |
from base import A, B |
| 14 |
from derived import C |
| 15 |
|
| 16 |
assert B().foo() == 'B.foo' |
| 17 |
assert C().foo() == 'B.foo' |
| 18 |
|
| 19 |
assert A().foo1() == 'A.foo' |
| 20 |
assert B().foo1() == 'B.foo' |
| 21 |
assert C().foo1() == 'B.foo' |
| 22 |
|
| 23 |
assert B().foo2() == 'B.foo' |
| 24 |
assert C().foo2() == 'B.foo' |
| 25 |
|
| 26 |
assert C().bar() == 'C.bar' |
| 27 |
|
| 28 |
######## base.pxd ######## |
| 29 |
cdef class A(object): |
| 30 |
cdef foo(self) |
| 31 |
|
| 32 |
cdef class B(A): |
| 33 |
cpdef foo(self) |
| 34 |
|
| 35 |
######## base.pyx ######## |
| 36 |
cdef class A(object): |
| 37 |
cdef foo(self): |
| 38 |
return "A.foo" |
| 39 |
|
| 40 |
def foo1(self): |
| 41 |
return self.foo() |
| 42 |
|
| 43 |
cdef class B(A): |
| 44 |
cpdef foo(self): |
| 45 |
return "B.foo" |
| 46 |
|
| 47 |
def foo2(self): |
| 48 |
return self.foo() |
| 49 |
|
| 50 |
######## derived.pyx ######## |
| 51 |
from base cimport B |
| 52 |
|
| 53 |
cdef class C(B): |
| 54 |
cpdef bar(self): |
| 55 |
return "C.bar" |