I am implementing multiple RL agents which share a lot of common attributes and methods but differ in only one. Namely the one that calculates the td_error. Out of the top of my head I can think of 3 options to implement this:
pass
on the one that is specific to each subclass.if else
to achieve the specific behavior.Here is what I don't like about each option:
I have been presented with this situation before and I normally go with option 2. but I am pretty sure there is a more correct way of achieving this.
You most definitely do not have to repeat yourself with Abstract class. If you define methods without decorating them as abstract, they will work just fine and can be used in child classes.
from abc import ABC, abstractmethod
class Polygon(ABC):
def amPolygon(self):
print("I am polygon")
@abstractmethod
def noofsides(self):
raise NotImplementedError
class Triangle(Polygon):
# overriding abstract method
def noofsides(self):
print("I have 3 sides")
class Pentagon(Polygon):
# overriding abstract method
def noofsides(self):
print("I have 5 sides")
R = Pentagon()
R.amPolygon()