Skip to content
Advertisement

Passing arguments to context manager

Is there a way to pass arguments using context manager? Here is what I’m trying to do:

JavaScript

But I am getting an error:

JavaScript

Class OrderStatusLock:

JavaScript

And if it is possible, what issues I can face, using this? Thank you very much.

Advertisement

Answer

There’s a lot going on in your question, and I don’t know where your _ContextManagerMixin class comes from. I also don’t know much about async.

However, here’s a simple (non-async) demonstration of a pattern where an argument can be passed to a context manager that alters how the __enter__ method of the context manager operates.

Remember: a context manager is, at its heart, just a class that implements an __enter__ method and an __exit__ method. The __enter__ method is called at the start of the with block, and the __exit__ method is called at the end of the with block.

The __call__ method added here in my example class is called immediately before the __enter__ method and, unlike the __enter__ method, can be called with arguments. The __exit__ method takes care to clean up the changes made to the class’s internal state by the __call__ method and the __enter__ method.

JavaScript

N.B. My example above will suppress all exceptions that are raised in the body of the with statement. If you don’t want to suppress any exceptions, or if you only want to suppress certain kinds of exceptions, you’ll need to alter the implementation of the __exit__ method.

Note also that the return values of these functions are quite important. __call__ has to return self if you want the class to be able to then call __enter__. __enter__ has to return self if you want to be able to access the context manager’s internal state in the body of the with statement. __exit__ should return True if you want exceptions to be suppressed, and False if you want an encountered exception to continue to endure outside of the with statement.

You can find a good tutorial on context managers here. The tutorial also contains some info on how you might adapt the above pattern for async code using the __aenter__ and __aexit__ methods.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement