4 ms·
> It’s also critical in Python to make heavy use of metaprogramming with decorators Interesting use case, thanks for sharing! Out of curiosity, when are some
by benrbray 6y ago
> It’s also critical in Python to make heavy use of metaprogramming with decorators
Interesting use case, thanks for sharing! Out of curiosity, when are some scenarios where it's "critical" to write code this way? I mostly interact with small pytorch / numpy codebases that don't grow large enough for this sort of thing.
- mlthoughts2018 6y agoPytest as a unit test framework is a great example. Some other good ones include the contextmanager decorator from contextlib, the retrying.retry decorator, gradual type enforcement decorators, decorators for control web app routing and HTTP / REST parametrics like in Flask. Even in hardcore numerical computing this way of working has huge wins over traditional OOP designs or plain flat function designs like C. For example see numba’s jit and autojit decorators that also work this way. For cases when you want to do it yourself, think of functions that wrap complex matplotlib or seaborn plotting utilities that rely on tons of optional parameters via kwargs. I often find it super helpful to do this when wrapping pandas functions too, which also involve tons of kwargs customization. FWIW I work professionally in deep learning and other ML. I think scientific computing in Python in particular highlights the importance of this.