これ (2.7.3) を実行すると、次の出力が得られます。
'Slotted1' object has no attribute 'c'
attribute c added to <class '__main__.Slotted2'>
Slotted1 と Slotted2 の動作の違いがわかりません。誰でも説明できますか?
from ctypes import *
class BracedStructure(Structure):
def __init__(self, **args):
super(BracedStructure,self).__init__(**args)
class Slotted1(Structure):
__slots__ = ['a','b']
_fields_ = [('a',c_int8),('b',c_int8)]
def __init__(self, **args):
super(Slotted1,self).__init__(**args)
def attemptToAddField(self):
self.c = '?'
print 'attribute c added to %s' % self.__class__
class Slotted2(BracedStructure):
__slots__ = ['a','b']
_fields_ = [('a',c_int8),('b',c_int8)]
def __init__(self, **args):
super(Slotted2,self).__init__(**args)
def attemptToAddField(self):
self.c = '?'
print 'attribute c added to %s' % self.__class__
if '__main__' == __name__:
s1 = Slotted1(a=1,b=2)
try:
s1.attemptToAddField()
except AttributeError as e:
print e
s2 = Slotted2(a=1,b=2)
try:
s2.attemptToAddField()
except AttributeError as e:
print e