object __new__函数
2019-02-13 本文已影响0人
阿发贝塔伽马
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))