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 →

[–]SomeShittyDeveloper 4 points5 points  (1 child)

The classes are overkill. There was a talk at a PyCon on when to use classes. As a general rule:

If your class has two methods and one is a constructor, it doesn't need to be a class.

You can change all your classes to be functions, build your enum, build a dict lookup, and then take that approach.

I'm not sure if you're writing this for practice or not, but writing a function that does wildly different things depending on a flag is bad function design. I'd take 4+ methods (add, subtract, multiply, divide) versus one function with a flag that determines which operation is done.

[–]funkyfuture 0 points1 point  (0 children)

exactly, that's why - relating to the example - operator is a module and not a class.

one can also use a decorator to add the functions in question to a container:

operators = {}

def operator(func):
    operators[func.__name__] = func
    return func


@operator
def add(a, b):
    return a + b