gpt4 book ai didi

python - Python 中的抽象类属性?

转载 作者:行者123 更新时间:2023-12-05 05:33:32 25 4
gpt4 key购买 nike

<分区>

我有这样一个类

from abc import ABC

class AbstractFoo(ABC):
# Subclasses are expected to specify this
# Yes, this is a class attribute, not an instance attribute
bar: list[str] = NotImplemented

# for example
class SpecialFoo(AbstractFoo):
bar = ["a", "b"]

但这感觉不是特别干净,也许还有点困惑。重要的是,bar 属性在任何地方都没有标记为抽象的,因此仍然可以在不指定的情况下实例化它。有没有更好的方法来实现类似的行为?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com