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

pythonz之__new__与__init__

相关文章:

  • 2021-06-06
  • 2021-10-10
  • 2022-12-23
  • 2023-02-24
  • 2021-06-22
  • 2021-04-26
  • 2022-01-26
猜你喜欢
  • 2021-07-06
  • 2021-09-29
  • 2021-11-10
  • 2021-05-18
  • 2021-09-10
  • 2022-02-19
相关资源
相似解决方案