RFC: Proposal: Deterministic Object Destruction
Ooomzay
ooomzay at gmail.com
Sat Mar 3 21:19:37 EST 2018
On Saturday, 3 March 2018 23:52:34 UTC, Steven D'Aprano wrote:
> I know that laziness and hubris are programmer virtues, but there is
> still such a thing as *too much laziness*. RAII works in C++ where
> instances are allocated in the stack, but even there, if you have an
> especially long-lived function, your resources won't be closed promptly.
> In Python terms:
>
> def function():
> x = open_resource()
> process(x)
> # and we're done with x now, but too lazy to explicitly close it
> sleep(10000) # Simulate some more work. Lots of work.
> return
> # and finally x is closed (2.8 hours after you finished using it)
>
> The answer in C++ is "well don't do that then". The answer is Python is,
> "don't be so lazy, just use a with statement".
The answer in C++ would be to say "don't be so lazy just put the x
manipulation in a function or sub-block". The answer with Python + this PEP would be "don't be so lazy just put the x manipulation in a function or explicitly del x" ...no new syntax.
> If you want deterministic closing of resources, with statements are the
> way to do it.
>
> def function():
> with open_resource() as x:
> process(x)
> # and x is guaranteed to be closed
What a palava!
More information about the Python-list
mailing list