[Python-Dev] PEP 340 - possible new name for block-statement

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Fri Apr 29 13:44:46 CEST 2005


Nick Coghlan a écrit :
> Python offers two variants on the basic iterative loop.
> 
>   "for NAME from EXPR:" enforces finalisation of the iterator. At loop 
> completion, a well-behaved iterator is always completely exhausted. This 
> form supports block management operations, that ensure timely release of 
> resources such as locks or file handles.
>   If the values being iterated over are not required, then the statement 
> may be simplified to "for EXPR:".
> 
>   "for NAME in EXPR:" skips the finalisation step. At loop completion, a 
> well-behaved iterator may still contain additional values. This form 
> allows an iterator to be consumed in stages.
> 
> 
> Regardless of whether you like the above or not, I think the PEP's 
> proposed use of 'as' is incorrect - it looks like the variable should be 
> referring to the expression being iterated over, rather than the values 
> returned from the iterator.
> 
> Cheers,
> Nick.
> 

Well, I would go a step further and keep only the for-loop syntax, 
mainly because I don't understand why there is two syntax for things 
that's so close we can merge them !

You can simply states that the for-loop call the "__error__" method of 
the object if available without invalidating any other property of the 
new for-loop (ie. as defined in the PEP 340).

One main reason is a common error could be (using the synchronised 
iterator introduced in the PEP):

for l in synchronised(mylock):
   do_something()

It will compile, run, never raise any error but the lock will be 
acquired and never released !

Then, I think there is no use case of a generator with __error__ in the 
for-loop as it is now. So, IMO, it is error-prone and useless to have 
two different syntaxes for such things.

Pierre

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68


More information about the Python-Dev mailing list