[英]Cython: share pure python module
我有一個純python模塊a.py
文件,其中包含一個用cython增強的類:
@cython.cclass
class Test
如何在另一個純python模塊b.py
使用此類? 我from a import Test
嘗試過from a import Test
但是cython編譯器告訴我在b.py
使用Test
任何地方Not a type
正如我在評論中所說,您可以通過使用.pxd
文件和.py
文件來指定類型來做到這一點。我意識到這並不像將所有內容都放在.py
文件中那樣優雅,但是我知道這是您能做的最好的事情。
a.py:
class Test:
pass
a.pxd:
cdef class Test:
pass
b.py:
# here I use Test in all the places I think you could want to use it:
# as a function argument
# as a variable in a function
# in a class
import a
def f(x):
return x
def g():
t = a.Test()
return t
class C:
pass
b.pxd:
import cython
cimport a
cpdef f(a.Test x)
@cython.locals(t=a.Test)
cpdef g()
cdef class C:
cdef a.Test t
您可以通過檢查生成的bc
文件來驗證它是否正確使用了類型信息。
作為參考,相關文檔為http://docs.cython.org/src/tutorial/pure.html#magic-attributes-within-the-pxd
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.