MikiBelavista
MikiBelavista

Reputation: 2728

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?

Upvotes: 2

Views: 1419

Answers (1)

MkWTF
MkWTF

Reputation: 1372

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.

Upvotes: 4

Related Questions