
283
7.8 特殊メソッド
7.8.4
累算代入文
表
7-8
累算代入文
累算代入文 メソッド
a += b __iadd__(self, other)
a -= b __isub__(self, other)
a *= b __imul__(self, other)
a /= b __itruediv__(self, other)
a //= b __ifloordiv__(self, other)
a %= b __imod__(self, other)
a **= b __ipow__(self, other[, modulo])
a <<= b __ilshift__(self, other)
a >>= b __irshift__(self, other)
a &= b __iand__(self, other)
a ^= b __ixor__(self, other)
a |= b __ior__(self, other)
累算代入文は、左辺のオブジェクトの対応するメソッドを呼び出します。
一般に、累算代入文の左辺が更新可能なオブジェクトなら、自分自身の値を、演算の結果で更新
します。
>>> L = M = [1, 2, 3] # L
と
M
は同じリストを指す変数
>>> L += ['spam', 'ham'] #
リストオブジェクトの末尾に追加。
>>> L
[1, 2, 3, 'spam', 'ham']
>>> L is M # L
と
M
は同じリストを指す変数
True
一方、更新不能なオブジェクトは、新しいオブジェクトを作成して、戻り値とします。 ...