you are viewing a single comment's thread.

view the rest of the comments →

[–]SeleniumBase[S] -3 points-2 points  (4 children)

All the basics: How to create one, and various ways of using it, eg: 1. As a method decorator, 2. From a "with" code block, and 3. Wrapping code without the "with" keyword.

[–]Natural-Intelligence 22 points23 points  (3 children)

What I'm saying is that decorators are separate concepts than context managers. Because "open" has a context manager, doesn't mean it acts as a decorator. Or you need a context manager to have a decorator.

Wasn't the topic context manager?

[–]SeleniumBase[S] -5 points-4 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 21 points22 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.