目から鱗なお話。とりあえずメモ。
クラスとインスタンス
>>> class C: ... def meth(self, arg): ... self.val = arg ... return self.val ... >>> arg = 9 >>> foo = C() >>> foo.meth(arg) 9 >>> C.meth(foo, arg) 9 >>> foo.meth(arg) == C.meth(foo, arg) True
myself
後からメソッド追加
>>> class C: ... pass ... >>> def meth(myself, arg): ... myself.val = arg ... return myself.val ... >>> C.meth = meth >>> >>> bar = C() >>> bar.meth(arg) 9 >>> C.meth(bar, arg) 9 >>> bar.meth(arg) == C.meth(bar, arg) True