|
|
|
|
|
by DemetriousJones
492 days ago
|
|
One of my biggest gripes with python is the fact that the only way to create a local scope for variables is with functions. I understand if statements not having their own scope for simplicity's sake, but the fact that `with` blocks don't is simply mind-bobbling to me.
```
with open("text.txt", 'w') as f:
f.write("hello world")
f.write("hello world") # at least the handle was automatically closed so it will give an IO error
``` |
|
One example would be a timing context manager:
Another example is mocks, where you want to inspect how many times a mock was called and with what arguments, after the mock context manager has finished.