I'm creating a sorting application where there is a base class, SortingAlgorithm()
, from which other classes (one for each sort type) are derived (BubbleSort()
, RadixSort()
, etc).
The base class contains two functions which are relevant here: sort()
and presort()
. The sort()
function performs the sort itself, and the presort()
performs a presort (executed prior to the actual sort, to test whether there is an improvement in performance). Presort()
can be one of two operations:
- We iterate over all subsequent pairs in our list sequentially, and flip them if they're in the wrong order
- Two sorting algorithms, A and B, are applied to the first and second halves of our input list, respectively.
My question is that in the second operation above, we must pass two instances of classes which derive from SortingAlgorithm
. How is this enforced, through type hints (or other)?
My current approach is having the two sorts passed within a sequence, and check that the elements are of type SortingAlgorithm
:
import abc
from typing import Sequence
class SortingAlgorithm(abc.ABC):
@staticmethod
def presort(input_list: list, algorithms: Sequence[SortingAlgorithm]) -> list:
# ...
@abc.abstractmethod
def sort(self, input_list: list) -> list:
# ...
However, this throws an unresolved reference error for SortingAlgorithm
. Passing filename.SortingAlgorithm
throws an error regarding circular import (since I have to import the containing file).
Have gone wrong here somewhere, or is there a way to do this?