Passing default list argument to dataclasses

From the dataclasses.field docs:

The parameters to field() are:

  • default_factory: If provided, it must be a zero-argument callable that
    will be called when a default value is needed for this field. Among
    other purposes, this can be used to specify fields with mutable
    default values, as discussed below. It is an error to specify both
    default and default_factory.

Your default_factory is not a 0-argument callable but a list, which is the reason for the error:

from dataclasses import dataclass, field
from typing import List

@dataclass
class Pizza():
    ingredients: List = field(default_factory=['dow', 'tomatoes'])  # <- wrong!

Use a lambda function instead:

@dataclass
class Pizza():
    ingredients: List = field(default_factory=lambda: ['dow', 'tomatoes'])

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)