For review: PEP 343: Anonymous Block Redux and Generator Enhancements

Nicolas Fleury nidoizo at yahoo.com
Sun Jun 5 00:34:05 EDT 2005


Ilpo Nyyssönen wrote:
> Nicolas Fleury <nid_oizo at yahoo.com_remove_the_> writes:
>>What about making the ':' optional (and end implicitly at end of current 
>>block) to avoid over-indentation?
>>
>>def foo():
>>    with locking(someMutex)
>>    with opening(readFilename) as input
>>    with opening(writeFilename) as output
>>    ...
> 
> 
> How about this instead:
> 
> with locking(mutex), opening(readfile) as input:
>     ...
> 
> So there could be more than one expression in one with.

I prefer the optional-indentation syntax.  The reason is simple (see my 
discussion with Andrew), most of the time the indentation is useless, 
even if you don't have multiple with-statements.  So in my day-to-day 
work, I would prefer to write:

def getFirstLine(filename):
     with opening(filename) as file
     return file.readline()

than:

def getFirstLine(filename):
     with opening(filename) as file:
         return file.readline()

But I agree that in the case of only one with-statement, that's no big deal.

Also, if multiple with-statements are separated by other indentating 
statements, your proposal doesn't help:

with locking(lock)
if condition:
     with opening(filename) as file
     for line in file:
         ...

would still be needed to be written:

with locking(lock):
     if condition:
         with opening(filename) as file:
             for line in file:
                 ...

Regards,
Nicolas




More information about the Python-list mailing list