Abstract variables in abc

More to the point you can define abstract properties:

from abc import ABC, abstractmethod

class BaseClass(ABC):
    # Abstract property (must be defined in subclass)
    @property
    @abstractmethod
    def required_property(self):
        pass

    @classmethod
    def provided_method(cls):
        print(cls.required_property)


class SubClass(BaseClass):
    # Implementing the abstract property
    required_property = "I am from subclass"

# Usage
SubClass.provided_method()  # I am from subclass


class BadSubClass(BaseClass):
    pass

BadSubClass()  # TypeError: Can't instantiate abstract class
1 Like