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:

try:
    # 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

StrEnum

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',).


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 details)

Uploaded Source

File details

Details for the file backports.strenum-1.1.1.tar.gz.

File metadata

  • Download URL: backports.strenum-1.1.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.56.1 CPython/3.9.1

File hashes

Hashes for backports.strenum-1.1.1.tar.gz
Algorithm Hash digest
SHA256 bc980eb0923a741f360af70fd97e122340e576945d354eb6745c5eb6f4d87554
MD5 c7406b4b6d9863410cd268969656921a
BLAKE2b-256 6d2cde698e3ba70bc2eba39597e91608ec1abab13a7442be6bcc91566cbf2314

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page