0

In C++, I often times will declare attributes in an abstract base class and not instantiate it in the constructor (it doesn't make much sense to do that for my use cases) because the instantiation is meant to occur in the subclasses.

In Python, I am wondering what I can achieve the analogous behavior when declarations without instantiation aren't really a thing in Python?

One approach I saw posted was using a type annotation

from abc import ABC, abstract method

class abstract_base_class(ABC):
  var : int

  # define some abstract methods

class inherited(abstract_base_class):
  def __init__(self):
    self.var = .....

I am wondering if there's a standard way to achieve what I want?

I also may be trying to apply some C++ practices that shouldn't be applied to Python. Would it make more sense to just not "declare" var at all in this case in abstract_base_class?

I don't think the linked question provides an answer for my second question. It provides a way to accomplish what I want to do, but I don't know if that's the standard way to do it or not.

24n8
  • 1,898
  • 1
  • 12
  • 25

0 Answers0