RFC: Proposal: Deterministic Object Destruction
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Mar 5 11:44:09 EST 2018
On Sun, 04 Mar 2018 16:58:38 -0800, Ooomzay wrote:
> Here is an example of a composite resource using RAII:-
>
> class RAIIFileAccess():
> def __init__(self, fname):
> print("%s Opened" % fname)
> def __del__(self):
> print("%s Closed" % fname)
>
> class A():
> def __init__(self):
> self.res = RAIIFileAccess("a")
>
> class B():
> def __init__(self):
> self.res = RAIIFileAccess("b")
>
> class C():
> def __init__(self):
> self.a = A()
> self.b = B()
>
> def main():
> c = C()
Looking at that code, my major thought is that there is far too much OO
design, not enough simplicity.
Perhaps I'm missing something, but I have no idea what benefit there is
in that style of code over:
with open('a') as a:
with open('b') as b:
process(a, b)
So long as you only process a or b inside the nested block, you are
guaranteed that they will be open.
And unlike your RAII example, they will be closed when you exit,
regardless of how many references to them you have, regardless of whether
an exception occurs or not, regardless of whether there are cycles or
whether they are globals or whether the interpreter is shutting down.
I think that at this point, you have convinced me that you want to impose
enormous costs on all Python interpreters *and* Python developers, in
order to allow you to write C++ code in Python rather than learn Pythonic
idioms like the with statement.
I don't think this is a good tradeoff.
--
Steve
More information about the Python-list
mailing list