I have a basic abstract class structure such as the following:
from abc import ABCMeta, abstractmethod
class BaseClass(metaclass=ABCMeta):
@property
@abstractmethod
def class_name(self):
pass
@property
@abstractmethod
def class_val(self):
pass
@abstractmethod
def foo(self):
pass
def bar(self):
# do something
class ChildClass1(BaseClass):
class_name = "Child1"
class_val = 1
def foo(self):
# do something else
class ChildClass2(BaseClass):
class_name = "Child2"
class_val = 2
def foo(self):
# do something else again
This works to basically do what I want, i.e., it lets me define variations of a base class which share the same implementation of the method bar() but have different implementations of foo(), with different class-specific attributes -- all of which must be implemented in the child classes, otherwise it raises the error TypeError: Can't instantiate abstract class ChildClass1 with abstract methods class_name (for example, if I try implement it without the class_name class attribute).
This is almost what I want. However I would like to know if there is any way I can force the child classes to have a string type for the class_name attribute, an int type for the class_val attribute, etc.?