hello,
is it legal to return inside a for loop
or am I obliged to break out of the loop before returning ?
thanks
yomgui 38 2018
yomgui <no*@valid.or g> 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: def f():
.... for i in range(20):
.... if i > 10: return i
.... print f()
11
<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Mike Meyer wrote: yomgui <no*@valid.or g> 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.
yomgui
yomgui <no*@valid.or g> writes: Mike Meyer wrote: yomgui <no*@valid.or g> 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.
<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Mike Meyer wrote: yomgui <no*@valid.or g> writes:
Mike Meyer wrote:
yomgui <no*@valid.or g> 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/
Steve Holden wrote: 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?
Interestingly, I just saw a thread over at TurboGears(or is it this
group, I forgot) about this multiple return issue and there are people
who religiously believe that a function can have only one exit point.
def f():
r = None
for i in range(20):
if i > 10:
r = 10
break
if r is None: something
else: return r bo****@gmail.co m wrote: Steve Holden wrote:
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?
Interestingly, I just saw a thread over at TurboGears(or is it this group, I forgot) about this multiple return issue and there are people who religiously believe that a function can have only one exit point.
def f(): r = None for i in range(20): if i > 10: r = 10 break if r is None: something else: return r
Well, I'm happy in this instance that practicality beats purity, since
the above is not only ugly in the extreme it's also far harder to read.
What are the claimed advantages for a single exit point? I'd have
thought it was pretty obvious the eight-line version you gave is far
more likely to contain errors than Mike's three-line version, wouldn't
you agree?
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/
Steve Holden wrote: Well, I'm happy in this instance that practicality beats purity, since the above is not only ugly in the extreme it's also far harder to read.
What are the claimed advantages for a single exit point? I'd have thought it was pretty obvious the eight-line version you gave is far more likely to contain errors than Mike's three-line version, wouldn't you agree?
I thought it is about easier to spot error(that the return value is
done in single place) but didn't give too much attention as I don't buy
it. I call them "gotophobia "
Steve Holden wrote: Interestingly, I just saw a thread over at TurboGears(or is it this group, I forgot) about this multiple return issue and there are people who religiously believe that a function can have only one exit point.
def f(): r = None for i in range(20): if i > 10: r = 10 break if r is None: something else: return r
To bonono, not Steve:
So if a function should religiously have only one exit point why does the
example you give have two exit points? i.e. the 'return r', and the implied
'return None' if you execute the 'something' branch.
Well, I'm happy in this instance that practicality beats purity, since the above is not only ugly in the extreme it's also far harder to read.
What are the claimed advantages for a single exit point? I'd have thought it was pretty obvious the eight-line version you gave is far more likely to contain errors than Mike's three-line version, wouldn't you agree?
Arguments include: any cleanup code you need when returning from a function
is all in one place (mostly applicable to languages where you have to do
your own memory management---see the recent AST discussion on the python
dev list); or it stops you accidentally forgetting to return a value (as
demonstrated above :^) )
In practice it is impossible to write code in Python (or most
languages) with only one return point from a function: any line could throw
an exception which is effectively another return point, so the cleanup has
to be done properly anyway.
Duncan Booth wrote: To bonono, not Steve:
So if a function should religiously have only one exit point why does the example you give have two exit points? i.e. the 'return r', and the implied 'return None' if you execute the 'something' branch.
I may have remember it wrong, but don't ask me. I am not the one
advocate of it. Oh, you mean the something. It is that I forgot what it
was, the original one did have one return.
Arguments include: any cleanup code you need when returning from a function is all in one place (mostly applicable to languages where you have to do your own memory management---see the recent AST discussion on the python dev list); or it stops you accidentally forgetting to return a value (as demonstrated above :^) )
The faintly remember it was about the last reason, or why it has been
brought up. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Chris |
last post by:
I think I already know that the answer is that this can't be done, but
I'll ask anyways.
Suppose you want to use an RDBMS to store messages for a threaded
message forum like usenet and then display the messages. A toy table
definition (that I've tried to make standards compliant) might look
like:
create table messages (
message_id integer,
|
by: Petr Bravenec |
last post by:
I have found that when I use the RETURN NEXT command in recursive function,
not all records are returned. The only records I can obtain from function
are records from the highest level of recursion. Does exist some
work-around?
Thanks
Petr Bravenec
example:
create table foo (
|
by: tony collier |
last post by:
To break out of a loop i have seen some people use RETURN instead of BREAK
I have only seen RETURN used in functions. Does anyone know why RETURN is
used instead of BREAK to kill loops?
|
by: Niall |
last post by:
I was compiling the function below and received this error. Putting a return
statement after the loop solves the problem I believe this is a compiler
bug, as this code will always leave the function within the loop.
protected DateTime GetDate()
{
for (int i = 0; i < 3; i++)
{
try
{
|
by: OutdoorGuy |
last post by:
Greetings,
I am attempting to compile the code below, but I am receiving an error
message when I do so. The error message is: "CSO161:
'Forloop.CalcAvg(int)': Not all code paths return a value". Any idea as
to what I'm doing wrong? I'm sure it's something simple.
Thanks in advance!
public class ForLoop
| |
by: Paul Hasell |
last post by:
Hi,
I'm trying to invoke a web method asynchronously but just can't seem to get
it to tell me when it has finished! Below is the code I am (currently) using:
private void btnUpload_Click(object sender, System.EventArgs e)
{
try
{
SOPWebService.Client uploader = new
|
by: semedao |
last post by:
Hi , I am using asyc sockets p2p connection between 2 clients.
when I debug step by step the both sides , i'ts work ok.
when I run it , in somepoint (same location in the code)
when I want to receive 5 bytes buffer , I call the BeginReceive and then
wait on AsyncWaitHandle.WaitOne()
but it is signald imidiatly , and the next call to EndReceive return zero
bytes length , also the buffer is empty.
here is the code:
public static byte...
|
by: Pedro Pinto |
last post by:
Hi there once more........
Instead of showing all the code my problem is simple.
I've tried to create this function:
char temp(char *string){
alterString(string);
return string;
|
by: =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?= |
last post by:
Hello,
I have a question about the infamous GOTO statement and the way to return a
result from a sub:
I have a sub that has to make some calls to external COM methods, and
because these methods can fail I have to check them to be running ok, like
this:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |