On Friday, March 2, 2018 at 5:29:54 AM UTC, Rustom Mody wrote:
> Please excuse if this has been addressed above and/or its too basic:
> What's the difference between RAII and python's with/context-managers?
They address the same problem but I am claiming that RAII achieves this in a
significantly more elegant/pythonic way without involving any special keywords
or methods. in summary _if_ the PEP was adopted and/or you are using CPython
today then:-
def riaa_file_copy(srcname, dstname):
src = RAIIFile(srcname, 'r')
dst = RAIIFile(dstname, 'w')
for line in src:
dst.write(line)
becomes equivalent to:
def pep343_file_copy(srcname, dstname):
with open(srcname, 'r') as src,
open(dstname, 'w') as dst:
for line in src:
dst.write(line)
RAII resource management is also simpler to implement only requiring existing
__init__ and __del__ methods (e.g. to open/close the underlying file) and the
resource objects are invariant. Which means the objects/managers do not need to
track the enter/exit state - as there is no way to access them when they are
not "open" in RAII.
--
https://mail.python.org/mailman/listinfo/python-list