Skip to main content

Base class for creating enumerated constants that are also subclasses of str

Project description

A backport of (copy and paste from) python 3.10’s StrEnum class for >=3.8.6:

Base class for creating enumerated constants that are also subclasses of str.

See the design discussion, and Ethan Furman’s first and second PR with this implementation.

A slightly different implementation would likely be compatible with lower python versions; PRs are welcome if they pass the test suite. The existing (reference) implementation should still be the one used on supported versions.

Install with pip install backports.strenum, and use with:

    # be ready for 3.10 when it drops
    from enum import StrEnum
except ImportError:
    from backports.strenum import StrEnum

class MyStrEnum(StrEnum):
    POTATO = "potato"
    ORANGE = "orange"
    SPADE = "spade"

MyStrEnum.POTATO == "potato"  # True
MyStrEnum.ORANGE.upper() == "ORANGE"  # True
str(MyStrEnum.SPADE) == "spade"  # True


The second variation of Enum that is provided is also a subclass of str. Members of a StrEnum can be compared to strings; by extension, string enumerations of different types can also be compared to each other. StrEnum exists to help avoid the problem of getting an incorrect member:

>>> class Directions(StrEnum):
...     NORTH = 'north',    # notice the trailing comma
...     SOUTH = 'south'

Before StrEnum, Directions.NORTH would have been the tuple ('north',).


Unlike other Enum’s, str(StrEnum.member) will return the value of the member instead of the usual "EnumClass.member".

Creating members that are mixed with other data types

When subclassing other data types, such as int or str, with an Enum, all values after the = are passed to that data type’s constructor. For example:

>>> class MyEnum(IntEnum):
...     example = '11', 16      # '11' will be interpreted as a hexadecimal
...                             # number
>>> MyEnum.example
<MyEnum.example: 17>

StrEnum and str.__str__

An important difference between StrEnum and other Enums is the __str__ method; because StrEnum members are strings, some parts of Python will read the string data directly, while others will call str(). To make those two operations have the same result, StrEnum.__str__ will be the same as str.__str__ so that str(StrEnum.member) == StrEnum.member is true.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

backports.strenum-1.1.1.tar.gz (10.6 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page