[Python-Dev] PEP 340 keyword: Extended while syntax

Ron Adam rrr at ronadam.com
Thu May 5 21:05:28 CEST 2005

Gustavo Niemeyer wrote:
> Greetings,
>>Reasoning: The block statement resembles a "while" block in some ways in 
>>that it is a conditional block that may be executed only once, or 
>>possibly not at all (or many times).  And the word "while" is also 
>>descriptive of how a block is used.
>>     while VAR1 from EXPR1():
>>         BLOCK
> This is an interesting propose, but for a different PEP.  

Maybe someone who is more familiar with submitting a PEP could submit it 
as a competing PEP to 340 then.

In the
> current propose VAR1 is not evaluated for truthness, and many of
> the usage examples doesn't even require it.

VAR1 isn't evaluated for truthfulness, but the expression as a whole is. 
  It just says, EXPR1 is a iterator, and VAR1 received a value from it. 
Evaluating to a bool makes it consistent with the 'while' statement 
usage and those checks are all-ready taking place in the block 
statement. Here they are explicit instead of implicit which adds to the 
readability. IMHO of course.

> This looks quite strange, for instance:
>    while dummy from locking(myLock):
>       # Do something

I thought of that, but I could get use to it.  The dummy helps make it 
readable although the value may not actually be used in the block.  One 
use with locks is to return a count of the current locks.  Useful for 
monitoring what the iterator is doing.

A shorter "while locking(myLock):" could be used, and the "dummy from" 
be optional.  In that case the returned None would be discarded.

	while [NAME from] ITERATOR():

Or it could be made explicit with:

	while None == locking(myLock):

Although I suppose this would look strange to some also. In this case, 
an explicit test is being made of the returned VAR1.  Testing for other 
values could be possible.

> And also, this would require a break necessarily:
>    while (foo, bar) from locking():
>       # Pass

If the iterator is written without a loop in it, it will only execute 
one yield, so the second time though it will end without returning a 

def locking():
        yield lock()
        release lock()

This will execute once in an extended while.

def locking():
        while True:
           yield new_lock()

This would need to be broken out of.

>>This will require a new keyword/operator 'from' to use in a 'from' 
> 'from' is already a keyword, btw.

Oh, um... need more sleep. ;-)

So no new keywords would be needed in this example, just an alternate 
use of an existing keyword.  Replace the above with...

 >>This will require *no* new keyword, the keyword/operator 'from' will 
have a new use in an extended while expression:

Since I tend to put imports at the top of my programs and never see 
'from' anywhere else, it didn't ring a bell.

Any reason why they both couldn't work?

Cheers, Ron_Adam

More information about the Python-Dev mailing list