[Python-Dev] PEP 340: Deterministic Finalisation (new PEP draft, either a competitor or update to PEP 340)
Nick Coghlan
ncoghlan at gmail.com
Sun May 8 14:21:22 CEST 2005
Paul Moore wrote:
> On 5/8/05, Jp Calderone <exarkun at divmod.com> wrote:
>
>> If such a construct is to be introduced, the ideal spelling would seem to be:
>>
>> for [VAR in] EXPR:
>> BLOCK1
>> finally:
>> BLOCK2
>
>
> While I have not been following this discussion at all (I don't have
> the energy or time to follow the development of yet another proposal -
> I'll wait for the PEP) this does read more naturally to me than any of
> the other contortions I've seen passing by.
Given this for loop syntax:
for VAR in EXPR:
BLOCK1
else:
BLOCK2
finally:
BLOCK3
And these semantics when a finally block is present:
itr = iter(EXPR1)
exhausted = False
try:
while True:
try:
VAR1 = itr.next()
except StopIteration:
exhausted = True
break
BLOCK1
if exhausted:
BLOCK2
finally:
try:
BLOCK3
finally:
itr_exit = getattr(itr, "__exit__", None)
if itr_exit is not None:
try:
itr.__exit__(TerminateBlock)
except TerminateBlock:
pass
"Loop on this iterator and finalise when done" would be written:
for item in itr:
process(item)
finally:
pass
If you just want the finally clause, without finalising the iterator, you write
it as you would now:
try:
for item in itr:
process(item)
finally:
finalisation()
I like it - I'll update the PEP redraft to use it instead of the 'del' idea.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.blogspot.com
More information about the Python-Dev
mailing list