Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using python generics and inheritance

I need to implement a bunch classes, that all need to do the same modification to the parent class. As a skilled C++ programmer, I would look for a template class. But this needs to be implemented in Python. Here I am a rookie. I found the "Generic" and has constructed this example to show the core in my problem. The real program is somewhat more complicated(using ReportLab to generate PDF documents). I need to subclass to benefit from the class framework, which leads me into trying to implement a Generic using inheritance.

The problem is that cannot understand how I can pass argument to the "super" class, which here I try to make a Generic.

First attempt:

super().__init__(*argv, **kwargs)

Second attempt:

super(G, self).__init__(*argv, **kwargs)

Third attempt:

T.__init__(*argv, **kwargs)

Here is the complete example:

from typing import Generic, TypeVar

class A():
    def __init__(self, i : int):
        self.i = i

    def draw():
        print(self.i)

class C():
    def __init__(self, s : str):
        self.s = s

    def draw():
        print(self.s)

T = TypeVar('T')


class G(Generic[T]):
    def __init__(self, *argv, **kwargs):
        super().__init__(*argv, **kwargs)
        
    def draw():
        # Here is some special going to happen
        super().draw()
        
        
gA = G[A](5)        
gB = G[B]('Hello')

gA.draw()
gB.draw()

The output that I get:

Traceback (most recent call last):
  File "<string>", line 34, in <module>
File "/usr/lib/python3.8/typing.py", line 729, in __call__
    result = self.__origin__(*args, **kwargs)
  File "<string>", line 28, in __init__
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
like image 405
Paul Groskopf Avatar asked Oct 19 '25 08:10

Paul Groskopf


1 Answers

You have understood Python generics wrong. A Generic, say Generic[T], makes the TypeVar T act as whatever you provide the instance with.

I found this page to be very useful with learning Generics.

like image 92
sean-7777 Avatar answered Oct 21 '25 23:10

sean-7777



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!