return in loop for ?
Steve Holden
steve at holdenweb.com
Thu Nov 24 00:44:11 EST 2005
Mike Meyer wrote:
> yomgui <not at valid.org> writes:
>
>>Mike Meyer wrote:
>>
>>>yomgui <not at valid.org> 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/
More information about the Python-list
mailing list