Suppose I have:
JavaScript
x
12
12
1
class Super:
2
def __init__(self,a):
3
self.a = a
4
@classmethod
5
def from_b(cls,b):
6
return cls(b.to_a())
7
8
class Regular(Super):
9
def __init__(self,b):
10
# how to set my super to the output of
11
super = super.from_b(b)
12
How do I correctly initialize the super class with the output of the super class method rather than init?
My OOP background is in C++ and I am continually getting into these scenarios due to the ability to overload constructors in C++, so a workaround for this would be awesome.
Advertisement
Answer
@shx2’s answer works but wastefully/awkwardly creates a throw-away Super
object just to initialize the new Regular
object with its a
attribute.
If you have control over the source of Super
, you can make the from_b
method create an instance of the given subclass, and have the subclass call the from_b
method in its __new__
method instead, so that a Regular
object can be both created and initialized directly:
JavaScript
1
14
14
1
class Super:
2
def __init__(self, a):
3
self.a = a
4
5
@classmethod
6
def from_b(cls, b):
7
obj = super().__new__(cls)
8
cls.__init__(obj, b.to_a())
9
return obj
10
11
class Regular(Super):
12
def __new__(cls, b):
13
return super().from_b(b)
14
so that the following assertions will pass:
JavaScript
1
6
1
from unittest.mock import Mock
2
3
obj = Regular(Mock())
4
assert type(obj) is Regular
5
assert obj.a.to_a.is_called()
6