3 ms·
You can actually do this as a context manager, I have done something very similar in the past. I created a dummy context manager with a unique name and used an
by mirashii 8y ago
You can actually do this as a context manager, I have done something very similar in the past. I created a dummy context manager with a unique name and used an importhook to do an AST rewrite to the body of the context manager. It's a bit of a hack (okay, a lot of a hack), but allows for some really neat patterns that context managers didn't allow.