18/12/15 04:19:08.36 3+Cjc/VL.net
>>240
関数もメソッド(レシーバを第1引数に束縛したもの)もオブジェクトだよ。
内蔵関数は扱いが少し特殊なので Python コードで定義した関数で説明する。
>>> class Foo:
... def bar(self, x): return x*2
...
>>> f=Foo()
>>> def baz(y): return y+1
...
関数 Foo.bar および baz は function クラスのオブジェクト。
メソッド f.bar は method クラスのオブジェクト。
>>> type(Foo.bar)
<class 'function'>
>>> type(baz)
<class 'function'>
>>> type(f.bar)
<class 'method'>
function クラス、method クラスはそのままでは名前で参照できないが
types モジュールの FunctionType, MethodType という名前がそれぞれを指している。
これを使って f.bar と同じことをオブジェクトを生成する式として書くことができる。
>>> from types import MethodType
>>> MethodType(Foo.bar, f)
<bound method Foo.bar of <__main__.Foo object at 0x801dd2278>>
>>> f.bar
<bound method Foo.bar of <__main__.Foo object at 0x801dd2278>>
>>> MethodType(Foo.bar, f) == f.bar
True