【发布时间】:2018-03-02 04:45:46
【问题描述】:
ctypes 有一个类方法from_buffer。我正在尝试向子类中的from_buffer() 添加一些自定义处理,但我无法调用super()。这是一个例子:
from ctypes import c_char, Structure
class Works(Structure):
_fields_ = [
("char", c_char),
]
class DoesntWork(Works):
@classmethod
def from_buffer(cls, buf):
print "do some extra stuff"
return super(DoesntWork, cls).from_buffer(buf)
print Works.from_buffer(bytearray('c')).char
print DoesntWork.from_buffer(bytearray('c')).char
这会导致错误:
c
do some extra stuff
Traceback (most recent call last):
File "superctypes.py", line 18, in <module>
print DoesntWork.from_buffer(bytearray('c')).char
File "superctypes.py", line 14, in from_buffer
return super(DoesntWork, cls).from_buffer(buf)
AttributeError: 'super' object has no attribute 'from_buffer'
我错过了什么?为什么 super 在这里不起作用?
【问题讨论】:
标签: python python-2.7 ctypes metaclass