Skip to content

Support functional API for Enum creation #2306

Closed
@gvanrossum

Description

@gvanrossum

The enum.Enum class (new in Python 3.4, also backported as enum34) supports several short forms of creating an Enum type, described under functional API:

A = Enum('A', 'x y z')
B = Enum('B', ('p', 'q'))

These are equivalent to

class A(Enum):
    x = 1
    y = 2
    z = 3
class B(Enum):
    p = 1
    q = 2

Some other syntactic variants are also allowed:

A = Enum('A', 'x,y,z')
A = Enum('A', 'x, y, z')
B = Enum('B', ['p', 'q'])

The values can also be expressed as a list of tuples:

[('cyan', 4), ('magenta', 5), ('yellow', 6)]

or a mapping:

{'chartreuse': 7, 'sea_green': 11, 'rosemary': 42}

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions