Python.use(better) #Vector: step04 -- def __sub__(v1, v2):
‖記事一覧‖ Python.use(better)《Python3.1》
def __sub__(v1, v2):
《著》森こねこ、小粒ちゃん+∞《監修》小泉ひよ子とタマゴ倶楽部
第0版♪2001/03/02 ● 第1版♪2003/05/25 ● 第2版♪2004/06/01 ● 第3版♪2009/02/28
課題を作成する過程を通して「数値演算」の理解を深めます。
※ Python1.5 で作成した例題を、Python3.1 で再構成しました。
事例:コードの解説
class Vector(object): ... def __add__(v1, v2): s = [e1+e2 for e1,e2 in zip(v1.elements, v2.elements)] return Vector(*s) def __sub__(v1, v2): s = [e1-e2 for e1,e2 in zip(v1.elements, v2.elements)] return Vector(*s) ## ---------------------------------------- local = locals() ex_vector3(local)
事例:モジュールを起動する
■ 全項目を確認する
全ステップの「項目」を確認するには、関数 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 isinstance(v2, Vector): 9: step09 -- def __rmul__(v1, v2): 10: step10 -- def __iter__(self): 11: step10x -- def __radd__(v1, v2): 12: step11x -- def __radd__(v1, v2): 13: step12 -- def __radd__(v1, v2): >>>
■ 各項目を実行する
各ステップの「動作」を確認するには、関数 do に実引数を指定します。
>>> do(4) >>> # -------------------------------------------------- step04 >>> 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) >>>
2つのインスタンス v1,v2 を生成するとともに、ベクトルの差 v1-v2, v2-v1 が得られます。
《余録》テストケース
def ex_vector3(local): #@:
ex_vector2(local)
source = '''
v1-v2
v2-v1
'''.split("\n")
do_it(source, local)
》こちらに移動中です《
↑TOP