python中類與函數的關係

<

classfunc

1 python中class和function的關係

python中的函數也可以這樣實現 原函數的實現如下:

def e2(*args, **kws):
    return 2

print(e2(1), dir(e2))

通過_call_實現的class,可以使用函數式的調用方式

class E:
    def __init__(self):
        print('E.__init__ called')

    def __call__(self, *args, **kws):
        print('E.__call__ called')
        return 2

e = E()
print(e(1),dir(e))

從上可以看出函數也可以視爲類的一個對象。

Date: 2013-04-15 16:18:40 中國標準時間


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章