Python: multiple calls to __init__() on the same instance

后端 未结 3 1551
一个人的身影
一个人的身影 2021-02-19 01:07

The __init__() function gets called when object is created. Is it ok to call an object __init__() function again, after its been created?



        
3条回答
  •  南方客
    南方客 (楼主)
    2021-02-19 01:48

    It's fine to call __init__ more than once on an object, as long as __init__ is coded with the effect you want to obtain (whatever that may be). A typical case where it happens (so you'd better code __init__ appropriately!-) is when your class's __new__ method returns an instance of the class: that does cause __init__ to be called on the returned instance (for what might be the second, or twentieth, time, if you keep "recycling" instances via your __new__!-).

提交回复
热议问题