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))

你可能感兴趣的:(object __new__函数)