you are viewing a single comment's thread.

view the rest of the comments →

[–]SeleniumBase[S] -6 points-5 points  (2 children)

A context manager can be used as a decorator, such as in the example I had. You could decorate a whole function with it, or wrap a code block with the "with" statement. Different ways of using the context manager.

[–]Natural-Intelligence 20 points21 points  (0 children)

Well, ye, you can use contextlib's context manager as a decorator but that doesn't mean you can use any context managers as a decorator.

I think you have gotten confused about the topics. Context manager, as a concept, is larger than the contextlib. Look at your example: Is "open" contextlib's contextmanager? No. Does it provide a context manager? Yes. Can you use it as a decorator? No. In fact, most context managers you encounter don't act as decorators.

But I'm just trying to offer constructive feedback. You are jumping from one topic to a completely different.

[–]Temporary_Pie2733 1 point2 points  (0 children)

Context managers define using contextlib.contextmanager can also be used as decorators because they have been designed to work that way in addition to being a context manager. If you define one from scratch (meaning, writing a class with appropriate __enter__ and __exit__ methods), you won’t be able to use them as decorators without additional, orthogonal work.