为什么类变量的类型是supclass的类型?

2024-04-25 22:19:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在这里,括号的内容表示超类。你知道吗

class Tag(enum.Enum):
    a = 1
    b = 2
if __name__ == '__main__":
    print(Tag.a)

输出如下:

Tag.a

将“Tag.a”替换为“Tag.a.value”后,得到如下输出:

1

为什么类变量的类型是超类的类型?我不懂密码。请尽可能明确地解释结果。你知道吗


Tags: name密码类型内容ifvaluemaintag
1条回答
网友
1楼 · 发布于 2024-04-25 22:19:06

The docs相当不错:

enum — Support for enumerations New in version 3.4.

Source code: Lib/enum.py

An enumeration is a set of symbolic names (members) bound to unique, constant values. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over.

[...]

Creating an Enum Enumerations are created using the class syntax, which makes them easy to read and write. An alternative creation method is described in Functional API. To define an enumeration, subclass Enum as follows:

>>>
>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...

Note Enum member values Member values can be anything: int, str, etc.. If the exact value is unimportant you may use auto instances and an appropriate value will be chosen for you. Care must be taken if you mix auto with other values.

Note Nomenclature The class Color is an enumeration (or enum)

The attributes Color.RED, Color.GREEN, etc., are enumeration members (or enum members) and are functionally constants.

The enum members have names and values (the name of Color.RED is RED, the value of Color.BLUE is 3, etc.)

Note Even though we use the class syntax to create Enums, Enums are not normal Python classes. See How are Enums different? for more details. Enumeration members have human readable string representations:

>>>
>>> print(Color.RED)
Color.RED

相关问题 更多 >

    热门问题