Re: [Python-Dev] PEP 315 - do while

2006-10-04 Thread Hans Polak

Hi Nick,

Yep, PEP 315. Sorry about that.

Now, about your suggestion 
 do:
 setup code
 while condition
 loop body
 else:
 loop completion code

This is pythonic, but not logical. The 'do' will execute at least once, so
the else clause is not needed, nor is the loop completion code.  The loop
body should go before the while terminator.

I'm bound to reiterate my proposal:

before loop code
do:
loop body
setup code
while condition
after loop code

Example (if you know there will be at least one val).
source.open()
do:
val = source.read(1)
process(val)
while val != lastitem
source.close()

The c syntax is:
do
{
 block of code
} while (condition is satisfied);

The VB syntax is:
do
block
loop while cond

Cheers  thanks for your reply,
Hans Polak.

-Original Message-
From: Nick Coghlan [mailto:[EMAIL PROTECTED] 
Sent: domingo, 01 de octubre de 2006 6:18
To: Hans Polak
Cc: python-dev@python.org
Subject: Re: [Python-Dev] PEP 351 - do while

Hans Polak wrote:
 Hi,
 
  
 
 Just an opinion, but many uses of the 'while true loop' are instances of 
 a 'do loop'. I appreciate the language layout question, so I'll give you 
 an alternative:
 
  
 
 do:
 
 body
 
 setup code
 
 while condition
 

I believe you meant to write PEP 315 in the subject line :)

To fully account for loop else clauses, this suggestion would probably need
to 
be modified to look something like this:

Basic while loop:

 setup code
 while condition:
 loop body
 setup code
 else:
 loop completion code


Using break to avoid code duplication:

 while True:
 setup code
 if not condition:
 loop completion code
 break
 loop body

Current version of PEP 315:

 do:
 setup code
 while condition:
 loop body
 else:
 loop completion code

This suggestion:

 do:
 setup code
 while condition
 loop body
 else:
 loop completion code

I personally like that style, and if the compiler can dig through a function

looking for yield statements to identify generators, it should be able to
dig 
through a do-loop looking for the termination condition.

As I recall, the main objection to this style was that it could hide the
loop 
termination condition, but that isn't actually mentioned in the PEP (and in 
the typical do-while case, the loop condition will still be clearly visible
at 
the end of the loop body).

Regards,
Nick.

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
 http://www.boredomandlaziness.org


This message contains information that may be privileged or confidential and is 
the property of the Capgemini Group. It is intended only for the person to whom 
it is addressed. If you are not the intended recipient,  you are not authorized 
to read, print, retain, copy, disseminate,  distribute, or use this message or 
any part thereof. If you receive this  message in error, please notify the 
sender immediately and delete all  copies of this message.

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-04 Thread Hans Polak

Ok, I see your point. Really, I've read more about Python than worked with
it, so I'm out of my league here.

Can I combine your suggestion with mine and come up with the following:

  do:
  setup code
  loop body
  while condition
  else:
  loop completion code

Cheers,
Hans. 


-Original Message-
From: Nick Coghlan [mailto:[EMAIL PROTECTED] 
Sent: lunes, 02 de octubre de 2006 12:48
To: Hans Polak
Cc: python-dev@python.org
Subject: Re: [Python-Dev] PEP 315 - do while

Hans Polak wrote:
 Hi Nick,
 
 Yep, PEP 315. Sorry about that.
 
 Now, about your suggestion 
  do:
  setup code
  while condition
  loop body
  else:
  loop completion code
 
 This is pythonic, but not logical. The 'do' will execute at least once, so
 the else clause is not needed, nor is the loop completion code.  The
loop
 body should go before the while terminator.

This objection is based on a misunderstanding of what the else clause is for

in a Python loop. The else clause is only executed if the loop terminated 
naturally (the exit condition became false) rather than being explicitly 
terminated using a break statement.

This behaviour is most commonly useful when using a for loop to search
through 
an iterable (breaking when the object is found, and using the else clause to

handle the 'not found' case), but it is also defined for while loops.

Regards,
Nick.

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
 http://www.boredomandlaziness.org


This message contains information that may be privileged or confidential and is 
the property of the Capgemini Group. It is intended only for the person to whom 
it is addressed. If you are not the intended recipient,  you are not authorized 
to read, print, retain, copy, disseminate,  distribute, or use this message or 
any part thereof. If you receive this  message in error, please notify the 
sender immediately and delete all  copies of this message.

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-04 Thread Hans Polak

Thanks for your reply Nick, and your support Michael. I'll leave the PEP
talk to you guys :)

Cheers,
Hans 

-Original Message-
From: Michael Foord [mailto:[EMAIL PROTECTED] On Behalf Of Fuzzyman
Sent: martes, 03 de octubre de 2006 12:00
To: Nick Coghlan
Cc: Hans Polak; python-dev@python.org
Subject: Re: [Python-Dev] PEP 315 - do while

Nick Coghlan wrote:

Hans Polak wrote:
  

Ok, I see your point. Really, I've read more about Python than worked with
it, so I'm out of my league here.

Can I combine your suggestion with mine and come up with the following:

  do:
  setup code
  loop body
  while condition
  else:
  loop completion code



In my example, the 3 sections (setup code, loop body and loop
completion 
code are all optional. A basic do-while loop would look like this:

   do:
   setup code
   while condition

(That is, setup code is still repeated each time around the loop - it's 
called that because it is run before the loop evaluated condition is
evaluated)
  


+1

This looks good.

The current idiom works fine, but looks unnatural :

while True:
if condition:
   break

Would a 'while' outside of a 'do' block (but without the colon) then be
a syntax error ?

'do:' would just be syntactic sugar for 'while True:' I guess.

Michael Foord
http://www.voidspace.org.uk

Cheers,
Nick.

  



This message contains information that may be privileged or confidential and is 
the property of the Capgemini Group. It is intended only for the person to whom 
it is addressed. If you are not the intended recipient,  you are not authorized 
to read, print, retain, copy, disseminate,  distribute, or use this message or 
any part thereof. If you receive this  message in error, please notify the 
sender immediately and delete all  copies of this message.

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-04 Thread Hans Polak

I'm against infinite loops -something religious :), which explains the call
for the do loop.

The issue about the parser is over my head, but the thought had occurred to
me. Now, it would not affect while loops inside do loops, wouldn't it?

Cheers,
Hans.

-Original Message-
From: Nick Coghlan [mailto:[EMAIL PROTECTED] 
Sent: martes, 03 de octubre de 2006 15:51
To: Fuzzyman
Cc: Hans Polak; python-dev@python.org
Subject: Re: [Python-Dev] PEP 315 - do while

Fuzzyman wrote:
 Nick Coghlan wrote:
 In my example, the 3 sections (setup code, loop body and loop
completion 
 code are all optional. A basic do-while loop would look like this:

   do:
   setup code
   while condition

 (That is, setup code is still repeated each time around the loop - it's

 called that because it is run before the loop evaluated condition is
evaluated)
  

 
 +1
 
 This looks good.

I'm pretty sure it was proposed by someone else a long time ago - I was 
surprised to find it wasn't mentioned in PEP 315.

That said, Guido's observation on PEP 315 from earlier this year holds for
me too:

  I kind of like it but it doesn't strike me as super important [1]

 The current idiom works fine, but looks unnatural :
 
 while True:
 if condition:
break

There's the rationale for the PEP in a whole 5 lines counting whitespace ;)

 Would a 'while' outside of a 'do' block (but without the colon) then be
 a syntax error ?
 
 'do:' would just be syntactic sugar for 'while True:' I guess.

That's the slight issue I still have with the idea - you could end up with 
multiple ways of spelling some of the basic loop forms, such as these 3 
flavours of infinite loop:

   do:
   pass # Is there an implicit 'while True' at the end of the loop body?

   do:
   while True

   while True:
   pass

The other issue I have is that I'm not yet 100% certain it's implementable 
with Python's parser and grammar. I *think* changing the definition of the 
while statement from:

while_stmt ::=
   while expression : suite
 [else : suite]
to

while_stmt ::=
   while expression [: suite
 [else : suite]]

And adding a new AST node and a new type of compiler frame block DO_LOOP 
would do the trick (the compilation of a while statement without a trailing 
colon would then check that it was in a DO_LOOP block and raise an error if
not).

Cheers,
Nick.

[1]
http://mail.python.org/pipermail/python-dev/2006-February/060711.html

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
 http://www.boredomandlaziness.org


This message contains information that may be privileged or confidential and is 
the property of the Capgemini Group. It is intended only for the person to whom 
it is addressed. If you are not the intended recipient,  you are not authorized 
to read, print, retain, copy, disseminate,  distribute, or use this message or 
any part thereof. If you receive this  message in error, please notify the 
sender immediately and delete all  copies of this message.

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-04 Thread Hans Polak

Please note that until == while not.

do:
code
until count  10

do:
code
while count = 10

Cheers,
Hans.
 


-Original Message-
From: Michael Foord [mailto:[EMAIL PROTECTED] On Behalf Of Fuzzyman
Sent: martes, 03 de octubre de 2006 16:29
To: Nick Coghlan
Cc: Hans Polak; python-dev@python.org
Subject: Re: [Python-Dev] PEP 315 - do while

Nick Coghlan wrote:

 [snip..]

 The current idiom works fine, but looks unnatural :

 while True:
 if condition:
break


 There's the rationale for the PEP in a whole 5 lines counting
 whitespace ;)

 Would a 'while' outside of a 'do' block (but without the colon) then be
 a syntax error ?

 'do:' would just be syntactic sugar for 'while True:' I guess.


 That's the slight issue I still have with the idea - you could end up
 with multiple ways of spelling some of the basic loop forms, such as
 these 3 flavours of infinite loop:

   do:
   pass # Is there an implicit 'while True' at the end of the loop
 body?

   do:
   while True

   while True:
   pass

Following the current idiom, isn't it more natural to repeat the loop
'until' a condition is met. If we introduced two new keywords, it would
avoid ambiguity in the use of 'while'.

do:
loop body
until condition

A do loop could require an 'until', meaning 'do' is not *just* a
replacement for an infinite loop. (Assuming the parser can be coerced
into co-operation.)

It is obviously still a new construct in terms of Python syntax (not
requiring a colon after 'condition'.)

I'm sure this has been suggested, but wonder if it has already been
ruled out. An 'else' block could then retain its current meaning
(execute if the loop is not terminated early by an explicit  break.)

Michael Foord
http://www.voidspace.org.uk


This message contains information that may be privileged or confidential and is 
the property of the Capgemini Group. It is intended only for the person to whom 
it is addressed. If you are not the intended recipient,  you are not authorized 
to read, print, retain, copy, disseminate,  distribute, or use this message or 
any part thereof. If you receive this  message in error, please notify the 
sender immediately and delete all  copies of this message.

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-04 Thread Guido van Rossum
You are all wasting your time on this. It won't go in.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-04 Thread Jeremy Hylton
On 10/4/06, Guido van Rossum [EMAIL PROTECTED] wrote:
 You are all wasting your time on this. It won't go in.

+1 from me.  Should you mark PEP 315 as rejected?

Jeremy


 --
 --Guido van Rossum (home page: http://www.python.org/~guido/)
 ___
 Python-Dev mailing list
 Python-Dev@python.org
 http://mail.python.org/mailman/listinfo/python-dev
 Unsubscribe: 
 http://mail.python.org/mailman/options/python-dev/jeremy%40alum.mit.edu

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-04 Thread Raymond Hettinger
I'll mark it as withdrawn.

Raymond

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf
Of Jeremy Hylton
Sent: Wednesday, October 04, 2006 8:44 AM
To: Guido van Rossum
Cc: Hans Polak; python-dev@python.org
Subject: Re: [Python-Dev] PEP 315 - do while

On 10/4/06, Guido van Rossum [EMAIL PROTECTED] wrote:
 You are all wasting your time on this. It won't go in.

+1 from me.  Should you mark PEP 315 as rejected?

Jeremy


 --
 --Guido van Rossum (home page: http://www.python.org/~guido/)
 ___
 Python-Dev mailing list
 Python-Dev@python.org
 http://mail.python.org/mailman/listinfo/python-dev
 Unsubscribe:
http://mail.python.org/mailman/options/python-dev/jeremy%40alum.mit.edu

___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/rhettinger%40ewtllc.co
m
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-03 Thread Nick Coghlan
Hans Polak wrote:
 Ok, I see your point. Really, I've read more about Python than worked with
 it, so I'm out of my league here.
 
 Can I combine your suggestion with mine and come up with the following:
 
   do:
   setup code
   loop body
   while condition
   else:
   loop completion code

In my example, the 3 sections (setup code, loop body and loop completion 
code are all optional. A basic do-while loop would look like this:

   do:
   setup code
   while condition

(That is, setup code is still repeated each time around the loop - it's 
called that because it is run before the loop evaluated condition is evaluated)

Cheers,
Nick.

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
 http://www.boredomandlaziness.org
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-03 Thread Fuzzyman
Nick Coghlan wrote:

Hans Polak wrote:
  

Ok, I see your point. Really, I've read more about Python than worked with
it, so I'm out of my league here.

Can I combine your suggestion with mine and come up with the following:

  do:
  setup code
  loop body
  while condition
  else:
  loop completion code



In my example, the 3 sections (setup code, loop body and loop completion 
code are all optional. A basic do-while loop would look like this:

   do:
   setup code
   while condition

(That is, setup code is still repeated each time around the loop - it's 
called that because it is run before the loop evaluated condition is evaluated)
  


+1

This looks good.

The current idiom works fine, but looks unnatural :

while True:
if condition:
   break

Would a 'while' outside of a 'do' block (but without the colon) then be
a syntax error ?

'do:' would just be syntactic sugar for 'while True:' I guess.

Michael Foord
http://www.voidspace.org.uk

Cheers,
Nick.

  


___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-03 Thread Nick Coghlan
Fuzzyman wrote:
 Nick Coghlan wrote:
 In my example, the 3 sections (setup code, loop body and loop 
 completion 
 code are all optional. A basic do-while loop would look like this:

   do:
   setup code
   while condition

 (That is, setup code is still repeated each time around the loop - it's 
 called that because it is run before the loop evaluated condition is 
 evaluated)
  

 
 +1
 
 This looks good.

I'm pretty sure it was proposed by someone else a long time ago - I was 
surprised to find it wasn't mentioned in PEP 315.

That said, Guido's observation on PEP 315 from earlier this year holds for me 
too:

  I kind of like it but it doesn't strike me as super important [1]

 The current idiom works fine, but looks unnatural :
 
 while True:
 if condition:
break

There's the rationale for the PEP in a whole 5 lines counting whitespace ;)

 Would a 'while' outside of a 'do' block (but without the colon) then be
 a syntax error ?
 
 'do:' would just be syntactic sugar for 'while True:' I guess.

That's the slight issue I still have with the idea - you could end up with 
multiple ways of spelling some of the basic loop forms, such as these 3 
flavours of infinite loop:

   do:
   pass # Is there an implicit 'while True' at the end of the loop body?

   do:
   while True

   while True:
   pass

The other issue I have is that I'm not yet 100% certain it's implementable 
with Python's parser and grammar. I *think* changing the definition of the 
while statement from:

while_stmt ::=
   while expression : suite
 [else : suite]
to

while_stmt ::=
   while expression [: suite
 [else : suite]]

And adding a new AST node and a new type of compiler frame block DO_LOOP 
would do the trick (the compilation of a while statement without a trailing 
colon would then check that it was in a DO_LOOP block and raise an error if 
not).

Cheers,
Nick.

[1]
http://mail.python.org/pipermail/python-dev/2006-February/060711.html

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
 http://www.boredomandlaziness.org
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-03 Thread Fuzzyman
Nick Coghlan wrote:

 [snip..]

 The current idiom works fine, but looks unnatural :

 while True:
 if condition:
break


 There's the rationale for the PEP in a whole 5 lines counting
 whitespace ;)

 Would a 'while' outside of a 'do' block (but without the colon) then be
 a syntax error ?

 'do:' would just be syntactic sugar for 'while True:' I guess.


 That's the slight issue I still have with the idea - you could end up
 with multiple ways of spelling some of the basic loop forms, such as
 these 3 flavours of infinite loop:

   do:
   pass # Is there an implicit 'while True' at the end of the loop
 body?

   do:
   while True

   while True:
   pass

Following the current idiom, isn't it more natural to repeat the loop
'until' a condition is met. If we introduced two new keywords, it would
avoid ambiguity in the use of 'while'.

do:
loop body
until condition

A do loop could require an 'until', meaning 'do' is not *just* a
replacement for an infinite loop. (Assuming the parser can be coerced
into co-operation.)

It is obviously still a new construct in terms of Python syntax (not
requiring a colon after 'condition'.)

I'm sure this has been suggested, but wonder if it has already been
ruled out. An 'else' block could then retain its current meaning
(execute if the loop is not terminated early by an explicit  break.)

Michael Foord
http://www.voidspace.org.uk
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-03 Thread Ron Adam
Nick Coghlan wrote:
 Fuzzyman wrote:
 Nick Coghlan wrote:
 In my example, the 3 sections (setup code, loop body and loop 
 completion 
 code are all optional. A basic do-while loop would look like this:

   do:
   setup code
   while condition

 (That is, setup code is still repeated each time around the loop - it's 
 called that because it is run before the loop evaluated condition is 
 evaluated)
  

 +1

 This looks good.
 
 I'm pretty sure it was proposed by someone else a long time ago - I was 
 surprised to find it wasn't mentioned in PEP 315.
 
 That said, Guido's observation on PEP 315 from earlier this year holds for me 
 too:
 
   I kind of like it but it doesn't strike me as super important [1]

I looked though a few files in the library for different while usage patterns
and there really wasn't as many while loops that would fit this pattern as I
expected. There are much more while loops with one or more exit conditions in
the middle as things in the loop are calculated or received.

So it might be smart to find out just how many places in the library it would
make a difference.

   Ron



___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] PEP 315 - do while

2006-10-02 Thread Nick Coghlan
Hans Polak wrote:
 Hi Nick,
 
 Yep, PEP 315. Sorry about that.
 
 Now, about your suggestion 
  do:
  setup code
  while condition
  loop body
  else:
  loop completion code
 
 This is pythonic, but not logical. The 'do' will execute at least once, so
 the else clause is not needed, nor is the loop completion code.  The loop
 body should go before the while terminator.

This objection is based on a misunderstanding of what the else clause is for 
in a Python loop. The else clause is only executed if the loop terminated 
naturally (the exit condition became false) rather than being explicitly 
terminated using a break statement.

This behaviour is most commonly useful when using a for loop to search through 
an iterable (breaking when the object is found, and using the else clause to 
handle the 'not found' case), but it is also defined for while loops.

Regards,
Nick.

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
 http://www.boredomandlaziness.org
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com