4 ms·
Before clicking the link I wondered how this would translate the block-based Ruby API into Python. Using Python's with statement is quite nice here, I think. T
by jack_jennings 11y ago
Before clicking the link I wondered how this would translate the block-based Ruby API into Python. Using Python's with statement is quite nice here, I think.
The with statement is one of my favorite additions to the language. It's a really flexible syntax to encapsulate control flow, exceptions, necessary cleanup steps… etc. etc.
- chronial 11y agoI would argue that python provides even simpler syntax to enable functionality like this. @experiment.with(new_function) def old_function() ...
- shoyer 11y agoAgreed -- a decorator seems like a much cleaner way to handle this.
- msellout 11y agoA decorator must be applied to a function or class, whereas a context manager can wrap an arbitrary section of code. So, it depends on how you want the tool to be used. I don't think there's a clear best design.
- buttscicles 11y agoThis was my thought as well, and it's also a bit more awkward if your function signatures differ. Luckily they aren't mutually exclusive and it should be easy to implement a decorator version.