0

I would like to know how to define the enum just like below I do in JAVA and C?

C:
typedef enum status{
OFFLINE = 1,
ONLINE = 2,
LOGIN =3
} STATUS_T;
sureone
  • 831
  • 2
  • 14
  • 25

2 Answers2

1
OFFLINE = 1
ONLINE = 2
LOGIN = 3

You can wrap it in a class if you want STATUS_T.OFFLINE style of access. Python is a dynamically typed language so the concept of an enum makes little sense, all you can do is have range of meaningful values you can set something to.

Apparently, an equivalent has been added in 3.4, see How can I represent an 'Enum' in Python?

Community
  • 1
  • 1
Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
1

Not really.

The most common idiom is to define a set of constants at the class level:

class PhonyEnum(object):
     OFFLINE = 1
     ONLINE = 2
     LOGIN = 3

It's up to you to use them as constants:

if result == PhonyEnum.ONLINE: 
   do_something

Others also do the same thing at the module level rather than the class level

theodox
  • 12,028
  • 3
  • 23
  • 36