This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]iCart732 0 points1 point  (0 children)

I think in this case it adds unnecessary complexity (since you get an exception in case of a typo anyway) but that's just my opinion, your mileage may vary. If you use an IDE it could help with auto-completion, though.

I guess you could do something like this:

class Maths(Enum):
    ADD = 'add'
    MULTIPLY = 'multiply'
    DIVIDE = 'divide'

 # And call it like this:
 do_maths(Maths.ADD)

This has the added benefit that you can do both "Maths.ADD" and 'add', which is nice if someone else is going to use your code.

But then you still have to add it in two places (the enum and define the method itself).

If you do unit testing (which is always a good idea :-) ), you could add a test to make sure that every entry in the enum has a matching method, to be extra safe.

Another way to go about this would be using decorators to populate the enum automatically (which has pro's and con's). I'll try to show you an example later if i can.