I'm enhancing an existing class that does some calculations in the __init__
function to determine the instance state. Is it ok to call __init__()
from __getstate__()
in order to reuse those calculations?
-
2If the class is yours, sure! I would recommend separating these calculations into a classmethod and calling that from both `__init__` and `__getstate_` – This company is turning evil. May 04 '14 at 21:04
-
I would second @Kroltan - I would move those calculations out into a separate instance/class method rather than call `__init__` again. – jonrsharpe May 04 '14 at 21:22
-
1I'm confused. Do you want to call `__init__` from `__setstate__` or `__getstate__`? – martineau May 04 '14 at 22:35
3 Answers
To summarize reactions from Kroltan and jonsrharpe:
Technically it is OK
Technically it will work and if you do it properly, it can be considered OK.
Practically it is tricky, avoid that
If you edit the code in future and touch __init__
, then it is easy (even for you) to forget about use in __setstate__
and then you enter into difficult to debug situation (asking yourself, where it comes from).
class Calculator():
def __init__(self):
# some calculation stuff here
def __setstate__(self, state)
self.__init__()
The calculation stuff is better to get isolated into another shared method:
class Calculator():
def __init__(self):
self._shared_calculation()
def __setstate__(self, state)
self._shared_calculation()
def _shared_calculation(self):
#some calculation stuff here
This way you shall notice.
Note: use of "_" as prefix for the shared method is arbitrary, you do not have to do that.

- 26,309
- 7
- 59
- 69

- 42,725
- 12
- 101
- 98
-
3
-
1@cz Remember: **__new__()** constructs objects. **__init__()** just initializes instance variables. You can even define a class `class A: pass` and create instances from it `a = A()` because of this. – adam.hendry Feb 22 '22 at 01:16
It's usually preferable to write a method called __getnewargs__
instead. That way, the Pickling mechanism will call __init__
for you automatically.
-
did not manage to make it work. read the doc and still. resorted to methods above – Oren Jun 09 '20 at 16:50
Another approach is to Customize the constructor class __init__
in a subclass. Ideally it is better to have to one Constructor class & change according to your need in Subclass
class Person:
def __init__(self, name, job=None, pay=0):
self.name = name
self.job = job
self.pay = pay
class Manager(Person):
def __init__(self, name, pay):
Person.__init__(self, name, 'title', pay) # Run constructor with 'title'
Calling constructors class this way turns out to be a very common coding pattern in Python. By itself, Python uses inheritance to look for and call only one __init__
method at construction time—the lowest one in the class tree.
If you need higher __init__
methods to be run at construction time, you must call them manually, and usually through the superclass name as in shown in the code above. his way you augment the Superclass constructor & replace the logic in subclass altogether to your liking
As suggested by Jan it is tricky & you will enter difficult debug situation if you call it in same class

- 26,309
- 7
- 59
- 69

- 159
- 2
- 7
-
This doesn't answer the original question which was about `__setstate__` calling `__init__`. – akaihola Dec 12 '14 at 13:58