Is there a simple way to create a data class with a list property items
that has default values [1, 2, 3]
such that I can assign it other values [4, 5, 6]
with the constructor MyStruct(items=[4, 5, 6])
?
If I run the following code I get error:
TypeError: init() got an unexpected keyword argument 'items'
from dataclasses import dataclass
@dataclass
class MyStruct:
item: str = "Hello"
items = [1, 2, 3] # I need a list with default values
struct = MyStruct(item="Bye") # No problem here
struct = MyStruct(items=[4, 5, 6]) # Error "unexpected keyword argument"
I tried assigning list
type to items
, and also using field
function, but had no luck. I want a simple solution, without writing a lot of code, like a custom __init__
method.