0

Here's the tea: I'm writing a small Monopoly game using python. I've made this little class family to represent the bills. There's a base abstract class called Bill which inherits from the abc.ABC class.

Code:

from abc import ABC, abstractmethod
import colorama
colorama.init(autoreset=True, strip=True)
class Bill(ABC):
  #Abstract Properties (must be overriden in subclasses)
  @property
  @abstractmethod
  def count(self):
    return 0
  @property
  @abstractmethod
  def color(self): # colorama background color
    return 0
  @property
  @abstractmethod
  def worth(self): # integer representing the bill's worth
    return 0

  # Dunder methods (The same across class family)
  def __add__(self, other):
    return self.worth + other.worth
  def __str__(self):
    return f" {self.color} {self.worth}"

class One(Bill):
  def __init__(self):
    self.__count = 0
    self.__color = "\033[0m"
    self.__worth = 0
    #super().init() ??
  # Override Abstract Methods
  @property
  def count(self):
    return self.__count
  @count.setter
  def count(self, amount):
    self.__count += 1
  @property
  def worth(self):
    return 1
  @property
  def color(self):
    return colorama.Back.WHITE

My question is whether my subclasses will inherit the Bill class's dunder methods. And if not, do I need to include super().__init__() in the One class's __init__ method?

Jamba
  • 59
  • 1
  • 5
  • 1
    Did you try to run it? If so, what happened? – Axe319 May 05 '21 at 18:48
  • @Axe319 I'm not finished with it. I'm asking because I don't want to have to rewrite half my program later on. I'm using several files here. – Jamba May 05 '21 at 19:52
  • the reason I asked is that I pasted the code you have here, instantiated `One` with `one = One()` and printed it with `print(one)` (which calls `__str__`). So in short, running the code you posted, answers your question. – Axe319 May 06 '21 at 09:48

0 Answers0