Emulating a parameterized type [/class] in python

27 Views Asked by At

I'd like to create a parameterized class in python and wondering if that is at all possible:

Here is a statically typed class:

class DAGNode(ABC):

    @abstractmethod
    def dependencies(self) -> list[int]:
        pass

I'd like to use a parameterized class like follows:

class DAGNode(ABC):

    ntype = int

    @abstractmethod
    def dependencies(self) -> list[ntype]:  # this will not work in python, what can we do?
        pass

Or another way that is how scala would handle it:

class DAGNode(ABC)[ntype]:  # also not possible in python

    @abstractmethod
    def dependencies(self) -> list[ntype]:
        pass

Is there any construct for python remotely similar?

Note: I was not aware of generics support in python - as shown in the linked question. It's really not known much: I've been around large python shops at large companies and a number of startups, used many dozens of python libraries large and small, and a dozen university courses using the language and it never surfaced. Probably it is used in core libraries that I do not tend to read.

0

There are 0 best solutions below