私はライブラリ(ここでpylibtiff
ホストされています)を使用していて、拡張しようとしています。次の方法で問題が発生しました。class TIFF(ctypes.c_void_p)
# lib has been loaded with the path to libtiff.so, or equivalent
libtiff = ctypes.cdll.LoadLibrary(lib)
class TIFF(ctypes.c_void_p):
@classmethod
def open(cls, filename, mode='r'):
""" Open tiff file as TIFF.
"""
tiff = libtiff.TIFFOpen(filename, mode)
if tiff.value is None:
raise TypeError ('Failed to open file '+`filename`)
return tiff
(フルクラスはこちら)
ここで予想される使用パターンは、次のclass TIFF
ようにファイルを開いてインスタンスを受け取ることです。
import libtiff
t = libtiff.TIFF.open('myfile.tiff')
# e.g. grab a numpy array of the data:
arr = t.read_image()
この魔法は何ですか?libtiffのC変数は、pythonlandに戻ってきて、自動的にインスタンスになるものとしてどのように入力されますか? おそらくこれはのサブクラス化と関係がありますTIFF*
class TIFF
ctypes.c_void_p
私はメソッドをオーバーライドしようとしているので尋ねますclass TIFF
:
import libtiff
class TIFFdifferent(libtiff.TIFF):
def read_image(self, verbose=False, different=False):
if not different:
return libtiff.TIFF.read_image(self, verbose)
# my different code ...
ただし、class TIFFdifferent
インスタンスを作成しようとすると、class TIFF
代わりに次のようになります。
In [3]: t = libtiffdifferent.TIFFdifferent.open('file.tiff')
In [4]: arr = t.read_image(different=True)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-8bdc24ec874c> in <module>()
----> 1 arr = t.read_image(different=True)
TypeError: read_image() got an unexpected keyword argument 'different'
In [5]: t.read_image?
Type: instancemethod
String Form:<bound method TIFF.read_image of <TIFF object at 0x10b67df80>>
File: /Library/Python/2.7/site-packages/libtiff/libtiff_ctypes.py
Definition: t.read_image(self, verbose=False)
Docstring:
Read image from TIFF and return it as an array.
In [6]: t
Out[6]: <TIFF object at 0x10b67df80>
だから、私がする必要があるのは、オーバーライドすることでもあります-コンストラクター、キャスト、または他の明示的なものなしで、 CからPythonインスタンスへopen
の魔法の変換を理解せずに行う方法がわかりません。TIFF*
class TIFF