How can I represent an Enum in Python?

0 votes
Hi folks. Im a developer from a C# bckground and Im trying to learn Python... uh.. I'd like to ask how can I represent the equivalent of enum in Python?
Dec 11, 2018 in Python by charlie_brown
• 7,710 points
39 views

1 answer to this question.

0 votes

Here is one implementation:

class Enum(set):
    def __getattr__(self, name):
        if name in self:
            return name
        raise AttributeError

Here is its usage:

Animals = Enum(["DOG", "CAT", "HORSE"])

print(Animals.DOG)
answered Dec 11, 2018 by ariaholic
• 7,320 points

Related Questions In Python

+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,710 points
1,318 views
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,710 points
62 views
0 votes
0 answers

how can i contribute to an existing open source project in python?

is there a different platform for this ...READ MORE

May 8 in Python by Waseem
• 3,480 points
6 views
0 votes
1 answer
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 128 views
+4 votes
6 answers
0 votes
1 answer

How can I print an Error in Python?

For Python 2.6 and later and Python ...READ MORE

answered Oct 12, 2018 in Python by ariaholic
• 7,320 points
26 views
0 votes
1 answer

How can I turn an input date into a readable string in Python?

The datetime class has a method strftime. strftime() ...READ MORE

answered Dec 11, 2018 in Python by ariaholic
• 7,320 points
29 views