-12

Here, the content of parenthesis means superclass.

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

output is as follows:

Tag.a

After I replaced "Tag.a" with "Tag.a.value", I got the output as follows:

1

Why is class variable' s type the superclass's type?

I can't understand the code. Please interpret the result as explicitly as possible.

lejlun
  • 4,140
  • 2
  • 15
  • 31
Ben
  • 1
  • 3

1 Answers1

3

The docs are quite good:

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
serv-inc
  • 35,772
  • 9
  • 166
  • 188