> I *must* do:
>
> with device_open() as device:
>device.do_something()
>
> Nevertheless, I _need_ to have a class
> where the device is opened in the __init__()
> and used in some methods.
>
> Any ideas?
Perhaps take a look at contextlib.ExitStack and see if you can do something
with it.
Read the Fine context manager documentation.
What “with with_expression as var” does is effectively:
ob = with_expression
var = ob.__enter__()
And then at the end of the with, does a
ob.__exit__()
(With some parameters to __exit__, that could just be None, None, None for the
simplest case).
On 26/11/2023 18.50, Dieter Maurer wrote:
Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100:
...
Apparently, the "with" context manager is not usable
in classes, at least not with __init__() & co.
You can use `with` in classes -- with any context manager.
However, you would usually not use
On 27/11/23 5:03 pm, Grant Edwards wrote:
I should probably have written "how to fool that into
working when he's not using a 'with' statement"
It should be possible to run the context protocol yourself.
Something like (warning, untested):
class MyDeviceWrapper:
def
On 27/11/23 9:03 am, Stefan Ram wrote:
Above, "have" is followed by another verb in "have been",
so it should be eligible for a contraction there!
Yes, "been" is the past participle of 'to be", so "I've been" is
fine.
--
Greg
--
https://mail.python.org/mailman/listinfo/python-list
On 2023-11-27, Grant Edwards via Python-list wrote:
> On 2023-11-26, Dieter Maurer via Python-list wrote:
>
>> If you do not have this case (e.g. usually if you open the file
>> in a class's `__init__`), you do not use a context manager.
>
> He knows that. The OP wrote that he wants to use that
On 2023-11-26, Dieter Maurer via Python-list wrote:
> If you do not have this case (e.g. usually if you open the file
> in a class's `__init__`), you do not use a context manager.
He knows that. The OP wrote that he wants to use that can
_only_ be used by a context manager, but he wants that
Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100:
> ...
>Apparently, the "with" context manager is not usable
>in classes, at least not with __init__() & co.
You can use `with` in classes -- with any context manager.
However, you would usually not use `with` with a file you have opened
in