简体   繁体   中英

How to pass List as argument to dataclass? TypeError: __init__() missing required positional arguments

I tried this

from dataclasses import dataclass
import pandas as pd

@dataclass
class First:
    code_event: str
    code_event_system: str
    company_id: int
    date_event: str
    date_event_real: str
    ecode_class: str
......

d = pd.read_json('my.json', lines=True)
a = d.values.tolist()

Output of a

[['S1933190', 'STATIC', 3, '2020-05-26 16:30:00.000', '2020-05-26 16:30:00.000', 525065, 86393, '',......]]

At this moment, my idea is to put list as an argument.

p = First(a)

I got TypeError

TypeError: __init__() missing 30 required positional arguments: 'code_event_system', 'company_id',

From terminal

>>> len(a)
1
>>> len(a[0])
30

How to solve this?

You can instanciate a class with a list of arguments by doing this: First(*a)

The asterisk is simply unpacking the list a into the arguments of the class constructor First .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM