2

I have a class that acts like an enum. I want to loop over his variables (enum's values)

class Demos(object):
    class DemoType(object):
        def __init__(self, name):
            self.name = name

        def __repr__(self):
            return self.name

    VARIABLE1 = DemoType("Car")
    VARIABLE2 = DemoType("Bus")
    VARIABLE3 = DemoType("Example")
    VARIABLE4 = DemoType("Example2")

I thought about using Role.__dict__, or vars(Role), but they contain not only the variables, but also the RoleType class and other attributes like __module__. __doc__ and more...

I also want it to be represented like this, mainly because it will add more variables to DemoType. variables other than name, So please try to find an answer this way.

Akshat Tripathi
  • 321
  • 2
  • 12
Weiner Nir
  • 1,435
  • 1
  • 15
  • 19

2 Answers2

1

Rather than reinvent an enum type, it would be better to use Python's Enum type (which has also been backported). Then your code could look like

class Demos(Enum):
    VARIABLE1 = "Car"
    VARIABLE2 = "Bus"
    VARIABLE3 = "Example"
    VARIABLE4 = "Example2"


--> for variable in Demos:
...    print variable
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
  • Can you use this method and add more to a variable? Like, doing `VARIABLE1`, and add name `Car`, and a description `"A type of vehicle with 4 wheels"`? – Weiner Nir Nov 01 '13 at 23:42
  • 1
    @Nirock: Yes, the new `Enum` type is very powerful. Check out [this answer](http://stackoverflow.com/a/19330461/208880) for an `AutoEnum` recipe that easily supports docstrings. – Ethan Furman Nov 02 '13 at 00:29
  • =\, I need more than that. My Enum represents `TF2 Roles`. So its like `Demoman`, `Soldier`, `Scout`, `Medic`. What I need is to add a value of max_players, for each role. Thats why I use this pattern: http://stackoverflow.com/questions/19542862/how-to-represent-enum-with-functions-in-python – Weiner Nir Nov 02 '13 at 09:06
  • 1
    @Nirock: Why don't you ask a new question, `How do I use Python's new Enum for ...`, list a bunch of the attributes and methods that you need/want, and then someone can help with that? – Ethan Furman Nov 02 '13 at 20:43
0

I found the answer, And its not a duplicate of How can I represent an 'Enum' in Python? at all. The answer is to create the following list by the following list comprehensive:

variables = [attr for attr in dir(Demos()) if not attr.startswith("__") and not callable(attr)]
print variables 

I can also create a function to do that for me this way:

class Demos(object):
    class DemoType(object):
        def __init__(self, name):
            self.name = name

        def __repr__(self):
            return self.name

    @classmethod
    def get_variables(cls):
        return [getattr(cls, attr) for attr in dir(cls) if not callable(getattr(cls, attr)) and not attr.startswith("__")]

    VARIABLE1 = DemoType("Car")
    VARIABLE2 = DemoType("Bus")
    VARIABLE3 = DemoType("Example")
    VARIABLE4 = DemoType("Example2")


for variable in Demos.get_variables():
    print variable
Community
  • 1
  • 1
Weiner Nir
  • 1,435
  • 1
  • 15
  • 19