Type()でクラス作りにて。。
これを見ていただきたい。
#!/src/env/bin python from ctypes import * class BBeer_recipe(Structure): _fields_ = [ ('amt_barley',c_int), ('amt_water',c_int), ] Beer_recipe = type('Beer_recipe',(Structure,), {'_fields_':[ ("amt_barley",c_int), ("amt_water",c_int) ] } ) bber = BBeer_recipe() ber= Beer_recipe() print bber._fields_ print ber._fields_
#実行結果
[('amt_barley', <class 'ctypes.c_int'>), ('amt_water', <class 'ctypes.c_int'>)] [('amt_barley', <class 'ctypes.c_int'>), ('amt_water', <class 'ctypes.c_int'>)]
全く一緒の物ができたってことは言わなくてもわかるよね?
けどさぁ、なんかあれ?って思う所あるよね?
typeの関数を継承する所で、 (Strucure,) ←ってなってるよね。
?あれ?
なんで 『, 』がついてるんだ。
っておもうかもしれないけど、
どうやら、 type関数の 第2引数は タプルじゃないと行けないらしい。
もし , がないと
Traceback (most recent call last): File "te01.py", line 17, in <module> ("amt_water",c_int) TypeError: type() argument 2 must be tuple, not _ctypes.PyCStructType
って怒られます。
てか、これを見ても , が足りないよな って思えませんでした。
で、Python師匠のひろきさんが
『 ひろき/hirokiky: typeの第二引数にはタプルが期待されてるってのはわかる?
ひろき/hirokiky: TypeError: type() argument 2 must be tuple
ひろき/hirokiky: not _ctypes.PyCStructType
ひろき/hirokiky: ってエラーに書いてる。これで、タプルじゃなくてPyCStructTypeが渡されてしまってるって分かるから。
注意深く読むことですよw』
とのことにより、 , をつけることがわかりました。
今回得るべき教訓は
『初めて見たエラー文もちゃんと読もうず』