In the following example, the list1 attribute of P represents a list of instances of A.
class A:
value1: str
class B(A):
value2: str
class P:
list1: List[A]
I want is to keep a list of child classes of A (not instances of A) in the variable list1. Is there a way to do it in python?
The solution is to use https://docs.python.org/3/library/typing.html#typing.Type.
from typing import List, Type
class P:
list1: List[Type[A]]
Now, list1 represents a list of classes extended from A.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With