10

Python クラスを .pyx ファイル内の拡張型に変換しました。このオブジェクトを別の Cython モジュールで作成できますが、静的型付けを行うことはできません

これが私のクラスの一部です:

cdef class PatternTree:

    cdef public PatternTree previous
    cdef public PatternTree next
    cdef public PatternTree parent
    cdef public list children

    cdef public int type
    cdef public unicode name
    cdef public dict attributes
    cdef public list categories
    cdef public unicode output

    def __init__(self, name, parent=None, nodeType=XML):
        # Some code

    cpdef int isExpressions(self):
        # Some code

    cpdef MatchResult isMatch(self, PatternTree other):
        # Some code

    # More functions...

.pxd ファイルを使用して宣言しようとしましたが、すべての関数で「C メソッド [一部の関数] が宣言されていますが、定義されていません」と表示されます。また、実装の関数で C のものを削除して、拡張クラスのように動作させようとしましたが、それもうまくいきませんでした。

これが私の.pxdです。

cdef class PatternTree:
    cdef public PatternTree previous
    cdef public PatternTree next
    cdef public PatternTree parent
    cdef public list children

    cdef public int type
    cdef public unicode name
    cdef public dict attributes
    cdef public list categories
    cdef public unicode output

    # Functions
    cpdef int isExpressions(self)
    cpdef MatchResult isMatch(self, PatternTree other)

ご協力いただきありがとうございます!

4

1 に答える 1

12

私はそれに対する修正を見つけました。解決策は次のとおりです。

.pyx で:

cdef class PatternTree:

    # NO ATTRIBUTE DECLARATIONS!

    def __init__(self, name, parent=None, nodeType=XML):
        # Some code

    cpdef int isExpressions(self):
        # Some code

    cpdef MatchResult isMatch(self, PatternTree other):
        # More code

.pxd:

cdef class PatternTree:
    cdef public PatternTree previous
    cdef public PatternTree next
    cdef public PatternTree parent
    cdef public list children

    cdef public int type
    cdef public unicode name
    cdef public dict attributes
    cdef public list categories
    cdef public unicode output

    # Functions
    cpdef int isExpressions(self)
    cpdef MatchResult isMatch(self, PatternTree other)

どの Cython モジュール (.pyx) でも、これを次の目的で使用します。

cimport pattern_tree
from pattern_tree cimport PatternTree

警告の最後の言葉: Cythonは相対インポートをサポートしていません。これは、実行元のメイン ファイルに相対的なモジュール パス全体を指定する必要があることを意味します。

これが誰かを助けることを願っています。

于 2013-07-19T20:34:24.627 に答える