相关疑难解决方法(0)

将默认列表参数传递给数据类

我想在我的班级中传递默认参数,但不知何故我遇到了问题:

from dataclasses import dataclass, field
from typing import List

@dataclass
class Pizza():
    ingredients: List = field(default_factory=['dow', 'tomatoes'])
    meat: str = field(default='chicken')

    def __repr__(self):
        return 'preparing_following_pizza {} {}'.format(self.ingredients, self.meat)
Run Code Online (Sandbox Code Playgroud)

如果我打字

>>> my_order = Pizza()
Traceback (most recent call last):
  File "pizza.py", line 13, in <module>
    Pizza()
  File "<string>", line 2, in __init__
TypeError: 'list' object is not callable
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

-------------------------------------------------- ------------------------- AttributeError Traceback(最近一次调用last)in()----> 1 Pizza.ingredients

AttributeError:类型对象'Pizza'没有属性'ingredients'

编辑:对于类实例我收到以下错误:

from dataclasses import dataclass, field
from typing import List

@dataclass
class Pizza():
    ingredients: …
Run Code Online (Sandbox Code Playgroud)

python oop type-hinting python-3.x

9
推荐指数
2
解决办法
4657
查看次数

标签 统计

oop ×1

python ×1

python-3.x ×1

type-hinting ×1