Python Glossary Русский

Python 3.6+enumfunction

enum.auto()

auto

Assigns values to the members of an enumeration automatically (consecutive numbers by default).

Syntax

from enum import auto
class X(Enum):
    A = auto()

Numbering starts at 1, not 0, which quietly breaks compatibility with external protocols that count from zero. Mixed with explicit values, the next auto() continues from the last numeric one, and inside a Flag it yields powers of two instead of +1. Use auto() only when the values themselves are irrelevant and names carry the meaning; once a value is stored or sent over the wire, spell it out.

Examples

from enum import Enum, auto
class Dir(Enum):
    NORTH = auto()
    SOUTH = auto()
    EAST = auto()
    WEST = auto()
Dir.NORTH.value # → 1
Dir.SOUTH.value # → 2
list(Dir) # → [<Dir.NORTH:1>, <Dir.SOUTH:2>, ...]

See also

Open in the full glossarydocs.python.orgReport an inaccuracy