object __new__函數

class Foo(object): def __new__(cls,*agrs, **kwds): print(agrs) #inst = super(Foo,cls).__new__(cls,*agrs, **kwds) 這樣寫會報錯,原因是父類object的__new__不支持那麼多參數 inst = super(Foo,cls).__new__(cls) print(inst) return inst def __init__(self, price=50): self.price = price def how_much_of_book(self, n): print(self) return self.price * n foo = Foo(40) print(foo.how_much_of_book(8))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章