Pythonを始めた頃にこれは面白いと思った。インスタンス化したオブジェクトにメソッドやメンバー変数を後付けできるということ。
ただこれを多用されると、継承の時にオリジナルのクラスを見てもメソッドがわからないという事態になり、結構困るのでできるだけ避けたい気がする。
>>> class MyClass: ... def myfunc(self): ... print "hello!" ... >>> myinstance = MyClass() # MyClassのインスタンスを作る >>> myinstance.myfunc() # myfuncは正しく実行できる hello! >>> myinstance.hoge() # hoge()はMyClassに定義されていない Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: MyClass instance has no attribute 'hoge' >>> myinstanse.hoge # hogeもMyClassに定義されていない Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'myinstanse' is not defined >>> def foo(): ... print "bar" ... >>> myinstance.hoge = foo # MyClass.hogeの変数を定義してfoo()を代入 >>> myinstance.hoge() # MyClass.hoge()が実行できる bar >>>