Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an array or a list in a Python @dataclass? [duplicate]

How can I declare an array (or at least list) in @dataclass? A something like below:

from dataclasses import dataclass

@dataclass
class Test():
    my_array: Array[ChildType]
like image 678
Шах Avatar asked Dec 02 '22 09:12

Шах


1 Answers

There is no Array datatype, but you can specify the type of my_array to be typing.List:

from dataclasses import dataclass
from typing import List

@dataclass
class Test:
    my_array: List[ChildType]

And from Python 3.9 onwards, you can conveniently just use list:

from dataclasses import dataclass

@dataclass
class Test:
    my_array: list[ChildType]
like image 191
ruohola Avatar answered Dec 04 '22 00:12

ruohola