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

4

17

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

instance = cls(p1=1, p2=2)
# some code
instance.__init__(p1=123, p2=234)
# some more code
instance.__init__(p1=23, p2=24)

why would anyone wanna call __init__() on an object that is already created?

good question. i wanna re-initialize the instance's fields.

Pejorative answered 27/1, 2010 at 6:31 Comment(2)
This is a terrible design. Folks trying to read and maintain your code will be baffled. Please use ordinary methods with ordinary names. Have __init__ call another method (e.g., reset) so it's clear what's going on.Fernandofernas
#45799399 could be the best answer to this question. To re-initialize, you could reset it.Pierian
S
15

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__!-).

Some additional guidance can be found here. Multiple calls to init during object initialization

Svoboda answered 27/1, 2010 at 6:48 Comment(0)
E
6

You can, but it's kind of breaking what __init__ is intended to do. A lot of Python is really just convention, so you might as well follow then and expect __init__ to only be called once. I'd recommend creating a function called init or reset or something which sets the instance variables, use that when you want to reset the instance, and have __init__ just call init. This definitely looks more sane:

x = Pt(1,2)
x.set(3,4)
x.set(5,10)
Exchange answered 27/1, 2010 at 6:58 Comment(1)
+1: please do not call __init__ It violates the expectations of everyone reading and maintaining your code. It's simply bad design.Fernandofernas
G
1

As far as I know, it does not cause any problems (edit: as suggested by the kosher usage of super(...).__init__(...)), but I think having a reset() method and calling it both in __init__() and when you need to reset would be cleaner.

Grimace answered 27/1, 2010 at 6:36 Comment(0)
F
0

From my tries, I figured out it would not cause an error if you call an object __init__ function again after it's being created. You will just reinitialize the object's attributes, and the previously defined __init__ would be overwritten. However, it's not common and meaningful to do so. It would also confuse other people when reading your code. If you want to reset your instance, you could do so by defining a separate method or function.

Fraser answered 2/12, 2023 at 0:17 Comment(2)
What does your answer add to previous answers, in particular https://mcmap.net/q/703088/-python-multiple-calls-to-__init__-on-the-same-instance ?Albacore
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewGuillemette

© 2022 - 2024 — McMap. All rights reserved.