美文网首页
object __new__函数

object __new__函数

作者: 阿发贝塔伽马 | 来源:发表于2019-02-13 19:12 被阅读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))
    

    相关文章

      网友评论

          本文标题:object __new__函数

          本文链接:https://www.haomeiwen.com/subject/jhtjeqtx.html