Der Fall für StrEnum in Python 3.11
Dec 21 2022
Mit dem Update auf Python 3.11 haben wir eine subtile Änderung im Verhalten von Enum festgestellt.
Mit dem Update auf Python 3.11 haben wir eine subtile Änderung im Verhalten von Enum festgestellt. Dies ist PEP 663 zu verdanken – Standardisierung der Verhaltensweisen von Enum str(), repr() und format() .
Vor Python 3.11
Vor Python 3.11 gab eine String-Aufzählung wie unten gezeigt den Wert eines Eintrags in der Aufzählung zurück, wenn sie über das Format oder einen F-String verwendet wurde, nicht jedoch beim impliziten Aufruf __str__().
# Python 3.10
from enum import Enum
class Foo(str, Enum):
BAR = "bar"
x = Foo.BAR
x # Outputs <Foo.BAR: 'bar'>
f"{x}" # Outputs 'bar'
"{}".format(x) # Outputs 'bar'
str(x) # Outputs 'Foo.BAR'
x.value # Outputs 'bar'Python 3.11
# Python 3.11
from enum import Enum
class Foo(str, Enum):
BAR = "bar"
x = Foo.BAR
x # Outputs <Foo.BAR: 'bar'>
f"{x}" # Outputs 'Foo.BAR'
"{}".format(x) # Outputs 'Foo.BAR'
str(x) # Outputs 'Foo.BAR'
x.value # Outputs 'bar'
In Python 3.11 wurde StrEnum zur Standardbibliothek hinzugefügt. Die Verwendung dieser Option anstelle des oben genannten Aufzählungsstils sorgt für ein offensichtlicheres Verhalten.
# Python 3.11
from enum import StrEnum, auto
class Foo(StrEnum):
BAR = auto()
x = Foo.BAR
x # Outputs <Foo.BAR: 'bar'>
f"{x}" # Outputs 'bar'
"{}".format(x) # Outputs 'bar'
str(x) # Outputs 'bar'
x.value # Outputs 'bar'

![Was ist überhaupt eine verknüpfte Liste? [Teil 1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































