Python 数据类:范围内的属性类型,但解释器无法识别

问题描述 投票:0回答:0

以下代码

from dataclasses import dataclass
from enum import Enum


@dataclass
class Foo:
    type: FooType


class FooType(Enum):
    BAR = 0,
    BAZ = 1


if __name__ == '__main__':
    foo = Foo(type=FooType.BAR)

给出以下错误

❯ python test.py
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    class Foo:
  File "test.py", line 7, in Foo
    type: FooType
          ^^^^^^^
NameError: name 'FooType' is not defined

当我认为不应该的时候。 FooType 在同一个文件中,我认为这意味着它应该在 Foo 内部时在范围内。

我很感激任何对此的见解,因为我很困惑。

python-dataclasses
© www.soinside.com 2019 - 2023. All rights reserved.