Python.use(better) #Vector: step13 -- raise(TypeError(s))
‖記事一覧‖ Python.use(better)《Python3.1》《復刻版》
raise(TypeError(s))
《著》森こねこ、小粒ちゃん+∞《監修》小泉ひよ子とタマゴ倶楽部
第0版♪2001/03/02 ● 第1版♪2003/05/25 ● 第2版♪2004/06/01 ● 第3版♪2009/02/28
課題を作成する過程を通して「数値演算」の理解を深めます。
※ Python1.5 で作成した例題を、Python3.1 で再構成しました。
事例:コードの解説
class Vector(object): ... # TypeError: unsupported operand type(s) for +: 'int' and 'Vector' def __radd__(self, other): #@: if isinstance(other, Vector): return self+other else: s = "unsupported operand type(s) for +: %r and %r"%( other.__class__.__name__, self.__class__.__name__, ) raise(TypeError(s))
■ #1: メソッド:__radd__
if isinstance(other, Vector): return self+other
メソッド __radd__ は、2項演算子 + に呼応して、2つのベクトルの「和」を表わすインスタンスを生成します。2項演算子 + の左項/右項に着目すると、
- 左項が Vector であるなら、左項と右項はそのままで、__add__ が呼び出されます。
- 左項が Vector でないなら、左項と右項を入れ替えて、__radd__ が呼び出され、
- 引数 self を介してベクトルを、引数 other を介してスカラーを参照できます。
すると、メソッド __radd__ は、2項演算子 + を再利用するだけで、簡潔に表現できます。
- 2項演算子 + は、メソッド __add__ に帰結します。
■ #2: raise 文
else:
s = "unsupported operand type(s) for +: %r and %r"%(
other.__class__.__name__,
self.__class__.__name__,
)
raise(TypeError(s))
右項 self が Vector のインスタンスでないと、演算子 + を適用できません。そのため、
- 文字列 s に、左項 other と右項 self が属するクラスの名前 __class__.__name__ を含めて、
- エラーメッセージ s を伴う、例外 TypeError を生成 raise します。
《NOTE》特殊メソッド __radd__:__radd__ が呼び出されると、左項/右項が入れ替わり、
- self を介して右項の値を、
- other を介して左項の値を、
それぞれに参照するので、注意が必要です。
事例:モジュールを起動する
■ 全項目を確認する
全ステップの「項目」を確認するには、関数 do を利用します。
$ python -i vector.py >>> do() 0: step00 -- class Vector(object): 1: step01 -- def __init__(self, *args): 2: step02x -- def __add__(v1, v2): 3: step03 -- return Vector(*s) 4: step04 -- def __sub__(v1, v2): 5: step05 -- def __neg__(self): 6: step06 -- def __mul__(v1, v2): 7: step07x -- sum(e1*e2 ...) 8: step08x -- if hasattr(v2, "elements"): 9: step09 -- def __rmul__(self, other): 10: step10 -- def __iter__(self): 11: step11x -- def __radd__(self, other): 12: step12x -- raise(TypeError(s)) 13: step13 -- raise(TypeError(s)) 14: step14 -- def _typeError(self, v1, v2, op): >>>
■ 各項目を実行する
各ステップの「動作」を確認するには、関数 do に実引数を指定します。
>>> do(12) >>> # -------------------------------------------------- step12x >>> v = Vector(); v () >>> v1 = Vector(3,4); v1 (3, 4) >>> v2 = Vector(5,-2); v2 (5, -2) >>> v1+v2 (8, 2) >>> v1-v2 (-2, 6) >>> v2-v1 (2, -6) >>> -v1 (-3, -4) >>> -v2 (-5, 2) >>> v1*v2 7 >>> v2*v1 7 >>> v1*3 (9, 12) >>> v2*(-2) (-10, 4) >>> 3*v1 (9, 12) >>> (-2)*v2 (-10, 4) >>> 2+v1 TypeError: unsupported operand type(s) for +: 'Vector' and 'int' >>> v2+5 TypeError: unsupported operand type(s) for +: 'Vector' and 'int' >>> 2-v1 TypeError: unsupported operand type(s) for -: 'int' and 'Vector' >>> v2-5 TypeError: unsupported operand type(s) for +: 'Vector' and 'int' >>>
■ 何が問題か
ここでは、
- 任意の整数とベクトルとを加減しようとすると、例外 TypeError を生成します。
- ベクトルと任意の整数とを加減しようとすると、例外 TypeError を生成します。
例外 TypeError に伴うエラーメッセージからは、利用者にとって意味のある情報が得られるようになりました。
ところが、その内容を精査すると、不適切な表現が含まれます。たとえば、
>>> v2+5 TypeError: unsupported operand type(s) for +: 'Vector' and 'int'
では、'Vector' と 'int' の順序が「逆」で不適切です。
》こちらに移動中です《
↑TOP