I am trying to write a base abstract class that has some properties that will be initialized using a constructor. So far I have this:
from abc import ABC, abstractmethod
class A(ABC):
def __init__(self, n, *params):
self.n = n
self.initialize_params(*params) #I want to do this in all subsclasses of A
def initialize_params(self, *params)
pass
#abstractmethod
def do(self):
pass
class B(A):
def __init__(self, m, n, *super_params):
self.m = m
super(A, self).__init__(n, *super_params)
def do(self):
print("this is B")
But this will throw TypeError because of instantiation of A in __init__ of B. What is the correct way of doing this?
You seem to have a couple of mistakes:
from abc import ABC, abstractmethod
class A(ABC):
def __init__(self, n, *params):
self.n = n
self.initialize_params(*params) #I want to do this in all subsclasses of A
def initialize_params(self, *params):
pass
#abstractmethod
def do(self):
pass
class B(A):
def __init__(self, m, n, *super_params):
self.m = m
super().__init__(n, *super_params) # correct way to use super() to call init
def do(self):
print("this is B")
b = B(1,2)
b.do()
Note that you missed self. infront of initialize_params() and you didn't seem to be using super() correctly.
Related
How to inherit all class 'A' attributes and methods, but 'b()'?
class A:
def __init__(self):
# attributes
pass
#classmethod
def b(cls):
# logic
pass
class B(A):
def __init__(self, **kwargs):
super().__init__(**kwargs)
def b(self):
# nothing
pass
do not use this old method( if there is another way to do it ):
class B(A):
def __init__(self, attributes):
super().__init__(self, attributes)
You can reimplement b() to raise an error:
class A:
def __init__(self):
# attributes
pass
#classmethod
def b(cls):
# logic
pass
class B(A):
def __init__(self, **kwargs):
super().__init__(**kwargs)
#classmethod
def b(cls):
raise TypeError("method b is not supported in class B")
Also, if b() is a classmethod, you should probably override it as a classmethod.
Put that method in a separate class and don't inherit it.
class A:
def __init__(self):
# attributes
pass
class A1:
#classmethod
def b(cls):
# logic
pass
class B(A):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Use multiple inheritance when you want that method.
class C(A,A1):
pass
I have two abstract Classes with two methods: getArea(width, height) and getPerimeter(width, height). Those methods are abstract too, so they can be (or not) implemented in a derived class (In my case they must be implemented). In C# I could write IRectangle.getArea(){} or IParallelogram.getPerimeter(){} and simply make implementation. How can I do this in Python? I think that I have to use something like super(IRectangle, self).getArea() but I am not sure how.
from abc import ABC, abstractmethod
class IRectangle(ABC):
#abstractmethod
def getArea(width, height):
pass
#abstractmethod
def getPerimeter(width, height):
pass
class IParallelogram(ABC):
#abstractmethod
def getArea(parHeight, parBase):
pass
#abstractmethod
def getPerimeter(parHeight, parBase):
pass
class Calculate (IParallelogram, IRectangle):
You would just make a class that inherits from the abstract class and overrides the abstract methods like you would write a regular method.
class A(ABC):
#abstractmethod
def f(self):
pass
class B(A):
def f(self):
print ('hello')
If you want to override it but make it do nothing:
class B(A):
def f(self):
pass
Your class hierarchy is upside down. First, you would define a more generic class like Shape as the (abstract) root of the hierarchy.
class Shape(ABC):
#abstractmethod
def getArea(self):
pass
#abstractmethod
def getPerimeter(self):
pass
Then, the concrete classes IRectangle and IParallelogram would implement getArea and getPerimeter appropriately.
class IRectangle(Shape):
def getArea(self):
...
def getPerimeter(self):
...
class IParallelogram:
def getArea(self):
...
def getPerimeter(self):
...
I can't seem to find information regarding what I'm trying to do, so I'm afraid the answer is "you can't do it" or "that's bad practice." But, here it goes:
Given the following:
Class A(object):
def __init__(self):
pass
def methoda(self):
return 1
Class C(object):
def __init__(self):
pass
def methodc(self):
return 2
import A, C
Class B(object):
def __init__(self, classC):
A.__init__(self)
if classC:
C.__init__(self)
def methodb(self):
return 2
Obviously, running:
b = A()
b.methoda()
Is going to crash with an error:
Unbound method __init()___ must be called with A class as first argument (got B instance instead)
However, I am basically looking for a way to make this work. My motivation:
There are classes (maybe in the future) that will duplicate a certain group of methods (say some fancy conversions). In an effort to reduce code, I'd like for the future classes to inherit the methods; but for legacy reasons, I don't want B to inherit C.
A couple solutions:
Don't use special methods directly:
class A(object):
def __init__(self):
self._init(self)
#staticmethod
def _init(self):
...actual initialization code here...
def methoda(self):
return 1
class C(object):
def __init__(self):
self._init(self)
#staticmethod
def _init(self):
...actual initialization code here...
def methodc(self):
return 2
import A, C
class B(object):
def __init__(self, classC):
A._init(self)
if classC:
C._init(self)
def methodb(self):
return 2
Or silly hacks involving copying from initialized objects:
import A, C
class B(object):
def __init__(self, classC):
vars(self).update(vars(A()))
if classC:
vars(self).update(vars(C()))
def methodb(self):
return 2
Note that none of these solutions will give access to methods from A or C on instances of B. That's just ugly. If you really need inheritance, use inheritance, don't do terrible things trying to simulate it poorly.
I ended up just inheriting multiple classes. It's not exactly the way I wanted it done, but it's cleaner and easier for the IDE to follow
Class A(object):
def __init__(self):
super(A, self).__init__()
pass
def methoda(self):
return 1
Class C(object):
def __init__(self):
super(C, self).__init__()
def _C_init(self):
# some init stuff
pass
def methodc(self):
return 1
Class B(A, C):
def __init__(self, use_C):
super(B, self).__init__()
if use_C:
self._C_init()
def methodb(self):
return 2
This program seems to do everything by the book, yet this issue cropped up: while a base class was being init'ed a member method was called that is overriden in the derived class and assumes that the derived class has been constructed.
Is there some best practice to protect against this?
#!/usr/bin/env python3
class A:
def __init__(self):
self.ax = 1
print(self)
def __repr__(self):
return "{} ax: {}".format(self.__class__.__name__, self.ax)
class B(A):
def __init__(self):
super().__init__()
self.bx = 10
def __repr__(self):
return super().__repr__() + " bx: {}".format(self.bx)
if __name__ == "__main__":
B()
And here's the error:
AttributeError: 'B' object has no attribute 'bx'
Generally, unless you really know what you are doing, you want to call the superclass initialization after everything your class needs to do is done. Same with this example, repr is trying to print self.bx before you initialize it. If you do
class B(A):
def __init__(self):
self.bx = 10
super().__init__()
def __repr__(self):
return super().__repr__() + " bx: {}".format(self.bx)
it works as expected
Edited:
Instead of doing computation on __init__, one idea may be to do that in a factory function/classmethod.
Example instead of doing:
class A:
def __init__(self, a, b):
self.a = a
self.b = b
self.initialize()
def initialize(self):
# do some things
Do:
class A:
def __init__(self, a, b):
self.a = a
self.b = b
#classmethod
def from_a_b(cls, a, b):
instance = cls(a, b)
instance.initialize()
return instance
I'm currently using this pattern to create a class C that inherits from A and B. I couldn't call super().__init__ from C since I would have to do the same in A and B, and the unexpected parameter would cause problems at the top level. I feel like this isn't very elegant. What is the proper way to do multiple inheritance in Python? I guess it is unusual to query the mro to find out if the superclass expects a parameter?
class A:
def __init__(self, something):
self.a = X(something)
def method_a(self):
self.a.go()
def method_ab(self):
self.a.go2()
class B:
def __init__(self, something):
self.b = X(something)
def method_b(self):
self.b.go()
def method_ab(self):
self.b.go2()
class C(A, B):
def __init__(self, something):
self.a_ = A(something)
self.b_ = B(something)
#property
def a(self):
return self.a_.a
#property
def b(self):
return self.b_.b
def method_ab(self):
for x in [self.a, self.b]:
x.method_ab()
The best solution I found was to use a base class to absorb the extra parameters:
class Base:
def __init__(self, something):
pass
def method_ab(self):
pass
class A(Base):
def __init__(self, something):
super().__init__(something)
self.a = X(something)
def method_a(self):
self.a.go()
def method_ab(self):
super().method_ab()
self.a.go()
class B(Base):
def __init__(self, something):
super().__init__(something)
self.b = X(something)
def method_b(self):
self.b.go()
def method_ab(self):
super().method_ab()
self.b.go()
class C(A, B):
pass