If I create an abstract class in python using abc with an abstractmethod as follows and create a concrete class which doesn't use def to override the abstractmethod but instead attempt to set the attribute in the init function, it continues to throw the error that we can't instantiate an abstract class. I found out that I can get around this problem by creating a dummy of the abstractmethod. Is this the recommended way?
from abc import ABC, abstractmethod
class MyAbstract(ABC):
def __init__(self):
pass
@abstractmethod
def hi(self):
pass
class Concrete(MyAbstract):
def __init__(self, selector):
if selector:
self.hi = self.hi_a
else:
self.hi = self.hi_b
def hi(self):
'''
Is it possible to not have to create this function?
'''
pass
def hi_a(self):
print('a')
def hi_b(self):
print('b')
__init__, you need an instance. I'd suggest moving the selector logic tohiitself.