pythonz之__new__与__init__

new

__new__是用来控制对象的生成过程,在对象生成之前
__init__是用来完善对象的
如果new方法不返回对象(return super().new(cls)),则不会调用init函数

class Test:
    def __new__(cls, *arg, **kw):
        print('new')
        return super().__new__(cls)
    
    def __init__(self):
        print('init')

test = Test()

猜你喜欢

转载自www.cnblogs.com/raind/p/10146842.html