newb question about @property

Steve D'Aprano steve+python at pearwood.info
Mon Oct 2 06:04:04 EDT 2017


On Mon, 2 Oct 2017 07:51 pm, Marko Rauhamaa wrote:

> Chris Angelico <rosuav at gmail.com>:
> 
>> On Mon, Oct 2, 2017 at 5:34 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>>> I have *seen* a semi-useful decorator in code once
>>> (@contextlib.contextmanager) but still would prefer explicit dunder
>>> methods.
>>
>> [...] I'm not sure where dunder methods come into this, though, as
>> they're completely unrelated.
> 
> A context manager must implement __enter__() and __exit__().
> @contextlib.contextmanager implements them for you.

Nobody is holding a gun to your head and forcing you to use @contextmanager. Its
a convenience, nothing more. You can still write your own __enter__ and
__exit__ methods if you prefer.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list