Mike Meyer wrote: > yomgui <[EMAIL PROTECTED]> writes: > >>Mike Meyer wrote: >> >>>yomgui <[EMAIL PROTECTED]> writes: >>> >>>>is it legal to return inside a for loop >>>>or am I obliged to break out of the loop before returning ? >>> >>>Try it and see: >> >>it is not because it does work on an implementation of python >>that it will work on any other platform or in the futur. > > > That's true no matter how you arrive at your answer. > But, to answer the specific question, there's nothing in the language definition that suggests the programmer should refrain from using a return inside a loop, so for any implementation to impose such a restriction would be a violation of the language definition.
Yomgui: I am not a language lawyer, but I think you can feel safe returning from inside a loop. Just as a matter of interest, how else would you propose to implement the functionality Mike showed: > >>>def f(): > > ... for i in range(20): > ... if i > 10: return i > ... > Python is supposed to cleanly express the programmer's intent. I can;t think of a cleaner way that Mike's - can you? regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb.com PyCon TX 2006 www.python.org/pycon/ -- http://mail.python.org/mailman/listinfo/python-list