0

Is it possible to implement enumerations in Python with string constants (or any other arbitrary data type). I would like to something as

class Color(Enum):
    black = "black as night"
    white = "white as snow"
    green = "green as gras"

The reason, not hard to guess, is to use the constants of the enumeration directly as strings.

shuhalo
  • 5,732
  • 12
  • 43
  • 60
  • 1
    Does this answer your question? [enum - getting value of enum on string conversion](https://stackoverflow.com/questions/24487405/enum-getting-value-of-enum-on-string-conversion) – AMC May 08 '20 at 18:38

1 Answers1

0

Yes, you can use strings in enumerations in Python, check here. Check also the official documentation on enumerations.

Enum were introduced in Python version 3.4.

WARNING : These links concern the Python version 3.8.3

Catalina Chircu
  • 1,506
  • 2
  • 8
  • 19
  • 3
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/26069982) – Gino Mempin May 09 '20 at 00:37
  • @GinoMempin: Indeed the link may change, yet I really do not know what else can I add to the description of my answer. The answer is all there. I shall try to add something more ... – Catalina Chircu May 09 '20 at 05:04
  • My post answers precisely the question. If someone finds my answer is not good : please edit the answer with the appropriate changes. Otherwise, you may write a better answer yourself. – Catalina Chircu May 09 '20 at 05:17