...
Apparently, the "with" context manager is not usable
in classes, at least not with __init__() & co.
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 `__init__`.
If a class defines `__enter__` and `__exit__` (i.e.
the "cntext manager protocol"), then its instances
can be used with the `with` statement.
The important use case for a context manager is the
situation:
set up a context (--> method `__enter__`)
perform some operations in this context (--> body of `with` statement)
tear down the context (--> method `__exit__`).
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.
The problem is I've some SDK of some device which
provides context manager *only* classes.
I've myself unclear ideas
I'll not see any reply
Everybody please excuse my being off topic. But it really
make is hard for me to read messages when contractions are
I've got another an.
, but (note that no other verb follows "have" directly):
I have been there.
I have all of them.
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.
On 2023-11-26, Dieter Maurer via Python-list <python-list@python.org> 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 <something> that can
_only_ be used by a context manager, but he wants that usage to be
spread over various methods of a class he's writing. So he's asking
how to fool that <something> into working when he's not using a
context manager.
I should probably have written "how to fool that <something> into
working when he's not using a 'with' statement"
Above, "have" is followed by another verb in "have been",
so it should be eligible for a contraction there!
On 27/11/23 9:03 am, Stefan Ram wrote:
Above, "have" is followed by another verb in "have been",Yes, "been" is the past participle of 'to be", so "I've been" is
so it should be eligible for a contraction there!
fine.
On Nov 27, 2023, at 12:24 PM, Piergiorgio Sartor via Python-list <python-list@python.org> wrote:
On 26/11/2023 18.50, Dieter Maurer wrote:
Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100:
...You can use `with` in classes -- with any context manager.
Apparently, the "with" context manager is not usable
in classes, at least not with __init__() & co.
However, you would usually not use `with` with a file you have opened
in `__init__`.
If a class defines `__enter__` and `__exit__` (i.e.
the "cntext manager protocol"), then its instances
can be used with the `with` statement.
The important use case for a context manager is the
situation:
set up a context (--> method `__enter__`)
perform some operations in this context (--> body of `with` statement)
tear down the context (--> method `__exit__`).
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.
Very clear, but what if the class is *not* "open()",
but something else _requiring_ using "with"?
How to do this in a "__init__()" of a class?
In other words, what if "open()" could *only* be used
with "with" and not just by assigning "fp = open()"?
The problem is I've some SDK of some device which
provides context manager *only* classes.
I *cannot* do:
device = device_open(...)
device.do_something()
device.close()
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?
bye,
--
piergiorgio
--
https://mail.python.org/mailman/listinfo/python-list
On Nov 27, 2023, at 12:24 PM, Piergiorgio Sartor via Python-list <python-list@python.org> wrote:
On 26/11/2023 18.50, Dieter Maurer wrote:
Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100:
...You can use `with` in classes -- with any context manager.
Apparently, the "with" context manager is not usable
in classes, at least not with __init__() & co.
However, you would usually not use `with` with a file you have opened
in `__init__`.
If a class defines `__enter__` and `__exit__` (i.e.
the "cntext manager protocol"), then its instances
can be used with the `with` statement.
The important use case for a context manager is the
situation:
set up a context (--> method `__enter__`)
perform some operations in this context (--> body of `with` statement)
tear down the context (--> method `__exit__`).
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.
Very clear, but what if the class is *not* "open()",
but something else _requiring_ using "with"?
How to do this in a "__init__()" of a class?
In other words, what if "open()" could *only* be used
with "with" and not just by assigning "fp = open()"?
The problem is I've some SDK of some device which
provides context manager *only* classes.
I *cannot* do:
device = device_open(...)
device.do_something()
device.close()
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?
bye,
--
piergiorgio
--
https://mail.python.org/mailman/listinfo/python-list
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?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 79:56:03 |
Calls: | 6,716 |
Files: | 12,247 |
Messages: | 5,357,923 |