RE: [Python-Dev] Acquire/release functionality
Paul Moore <lists@morpheus.demon.co.uk> writes:
As far as the acquire/release case is concerned, I think that there is definitely a strong case for syntactic support for this. The equivalent idiom in C++ is "resource acquisition is initialisation" which, while not having syntax support, does rely on the existence of deterministic destructors, and on the ability to introduce a new scope at will. Python has neither of these, and the try...finally construct is verbose enough to make something better worth having.
In trying to draw a parallel between "with" and the C++ RAII idiom, I hit a funny, best illustrated by an example: class autoclose(file): __exit__ = close with f = autoclose("blah.txt", "r"): # Let's get sneaky g = f # Main code here # File now gets closed with g: # Who's a naughty boy then - g is a closed file # And the g.__exit__() call goes boom here... OK, it's contrived. The equivalent C++ risk is that of creating a dangling pointer to a local variable which is deleted, which is also rare. I don't know if this is a significant issue - after all, I can write code which closes a closed file object in normal Python. But I do think that we should remember the weird edge cases when discussing this sort of thing. If nothing else, it strikes me as a mild argument in favour of allowing the assignment to be part of the "with" clause, just so that people don't get used to seeing a bare "with g" (which doesn't include any obvious clues that g *needs* auto-cleanup). Paul.
On Monday 03 February 2003 03:31 pm, Moore, Paul wrote: ...
# File now gets closed with g: # Who's a naughty boy then - g is a closed file # And the g.__exit__() call goes boom here...
Why does it go boom?
I don't know if this is a significant issue - after all, I can write code which closes a closed file object in normal Python. But I do think
Sure:
f=file('noproblemo','w') f.close() f.close() f.close()
...and it gives absolutely no problem. We can close it a few more times too. I *LIKE* this: a termination-method SHOULD be idempotent (a no-op when called more than once), so that if various ways to "ensure the termination method is called" happen to be used all at once, that doesn't break anything. In this case, I think convenience should trump "errors shouldn't pass silently" -- i.e. I'd much rather define multiple calls to the terminator as "not an error", just like the built-in file object does!
If nothing else, it strikes me as a mild argument in favour of allowing the assignment to be part of the "with" clause, just so that people don't get used to seeing a bare "with g" (which doesn't include any obvious clues that g *needs* auto-cleanup).
Heh, good try (and I *WOULD* definitely like it if the optional assignment could be part of the with syntax, since I think there will be an assignment needed more often than not), but I'm not sure this specific argument is that strong a support for our shared preference;-). Alex
On Mon, Feb 03, 2003, Moore, Paul wrote:
In trying to draw a parallel between "with" and the C++ RAII idiom, I hit a funny, best illustrated by an example:
class autoclose(file): __exit__ = close
with f = autoclose("blah.txt", "r"): # Let's get sneaky g = f # Main code here
# File now gets closed with g: # Who's a naughty boy then - g is a closed file # And the g.__exit__() call goes boom here...
OK, it's contrived. The equivalent C++ risk is that of creating a dangling pointer to a local variable which is deleted, which is also rare.
It's not at all contrived. And I think it's perfectly acceptable; it's no different, fundamentally, from calling f.close() twice by mistake. -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ "Argue for your limitations, and sure enough they're yours." --Richard Bach
Moore, Paul wrote:
Paul Moore <lists@morpheus.demon.co.uk> writes:
As far as the acquire/release case is concerned, I think that there is definitely a strong case for syntactic support for this. The equivalent idiom in C++ is "resource acquisition is initialisation" which, while not having syntax support, does rely on the existence of deterministic destructors, and on the ability to introduce a new scope at will. Python has neither of these, and the try...finally construct is verbose enough to make something better worth having.
In trying to draw a parallel between "with" and the C++ RAII idiom, I hit a funny, best illustrated by an example:
class autoclose(file): __exit__ = close
with f = autoclose("blah.txt", "r"): # Let's get sneaky g = f # Main code here
# File now gets closed with g:
I haven't found a way to make assignments optional (in recent experiments). Is this possible with Python's Parser? holger
holger krekel <pyth@devel.trillke.net>:
I haven't found a way to make assignments optional (in recent experiments). Is this possible with Python's Parser?
It should be possible to do it the same way as expressions/assignments are done now, i.e. parse it as with_stmt ::= 'with' <expr> ['=' <expr>] ':' <suite> and sort it out in the compiler. Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+
participants (5)
-
Aahz -
Alex Martelli -
Greg Ewing -
holger krekel -
Moore, Paul