Closed cmc333333 closed 7 years ago
It looks very pythonic! It seems more minimal then the previous example. So here is a good open question - is there a performance difference that might matter for non simple examples? I'm going to run some experiments.
Low risk and this has been open for a bit, so I'm merging.
When removing duplicated chunks of logic, we tend to replace with declarative code (i.e. just indicating the parts that are unique to each situation). That tends to make heavy use of an abstract base class:
That works okay, but includes a lot of boiler plate and isn't a great use case for classes (as every class is effectively a singleton). I'd argue there are multiple levels of indirection, too, which make reading the code more difficult.
Here, I propose an alternative based on decorators, inspired by py.test's heavy decorator use. The decorators create a little DSL, significantly altering the wrapped function (notably, changing its arguments and adding extra logic). That makes reading the logic more challenging than if it were all in-lined, but I think better than the abc situation above. To replicate the above example, we'd have:
What do you think? Is this worth pursuing?