For review: PEP 343: Anonymous Block Redux and Generator Enhancements
Steven Bethard
steven.bethard at gmail.com
Sat Jun 4 17:24:36 EDT 2005
Andrew Dalke wrote:
> On Sat, 04 Jun 2005 10:43:48 -0600, Steven Bethard wrote:
>
>>Ilpo Nyyssönen wrote:
>>
>>>How about this instead:
>>>
>>>with locking(mutex), opening(readfile) as input:
>>> ...
>
>>I don't like the ambiguity this proposal introduces. What is input
>>bound to?
>
> It would use the same logic as the import statement, which already
> supports an 'as' like this
Ahh, so if I wanted the locking one I would write:
with locking(mutex) as lock, opening(readfile) as input:
...
There was another proposal that wrote this as:
with locking(mutex), opening(readfile) as lock, input:
...
which is what was confusing me. Mirroring the 'as' from the import
statement seems reasonable.
But it doesn't address my other concern, namely, is
with locking(mutex), opening(readfile) as input:
...
equivalent to the nested with-statements, e.g.:
_locking = locking(mutex)
_exc1 = (None, None, None)
_locking.__enter__()
try:
try:
_opening = opening(readfile)
_exc2 = (None, None, None)
input = _opening.__enter__()
try:
try:
...
except:
_exc2 = sys.exc_info()
raise
finally:
_opening.__exit__(*exc)
except:
_exc1 = sys.exc_info()
raise
finally:
_locking.__exit__(*exc)
Or is it equivalent to something different, perhaps:
_locking = locking(mutex)
_opening = opening(readfile)
_exc = (None, None, None)
_locking.__enter__()
input = _opening.__enter__()
try:
try:
...
except:
_exc = sys.exc_info()
raise
finally:
_opening.__exit__(*exc)
_locking.__exit__(*exc)
Or maybe:
_locking = locking(mutex)
_opening = opening(readfile)
_exc = (None, None, None)
_locking.__enter__()
input = _opening.__enter__()
try:
try:
...
except:
_exc = sys.exc_info()
raise
finally:
# same order as __enter__ calls this time!!
_locking.__exit__(*exc)
_opening.__exit__(*exc)
All I'm saying is that any of these are possible given the syntax. And
I don't see a precedent in Python for preferring one over another.
(Though perhaps there is one somewhere that I'm missing...)
And if it *is* just equivalent to the nested with-statements, how often
will this actually be useful? Is it a common occurrence to need
multiple with-statements? Is the benefit of saving a level of
indentation going to outweigh the complexity added by complicating the
with-statement?
STeVe
More information about the Python-list
mailing list