What can `__init__` do that `__new__` cannot?

前端 未结 3 2032
星月不相逢
星月不相逢 2020-12-06 04:26

In Python, __new__ is used to initialize immutable types and __init__ typically initializes mutable types. If __init__ were removed f

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 04:38

    Everything you can do in __init__ can also be done in __new__.

    Then, why use __init__?
    Because you don't have to store instance in variable (obj in your example code), and later bother returning it. You can focus on what you realy want to do – initializing mutable object.

提交回复
热议问题