By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,547 Members | 1,410 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,547 IT Pros & Developers. It's quick & easy.

a question about "return"

P: n/a
In one of the examples in the book I'm reading, it says:

def __init__(self):
...
...
...
return

It has nothing after "return". I expected it to have some number like 0
or 1.

What does it mean to have nothing after return?

Why do we even include "return" if we are not putting any value after?

Thanks :)

Regards,

Brian

Jul 4 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On 2006-07-04, yaru22 <ya****@gmail.comwrote:
What does it mean to have nothing after return?
It means to return None.
Why do we even include "return" if we are not putting any
value after?
If it's the last statement in the function, you don't need it.
Some people do it just as a formal indicate that that's the end
of the function.

OTOH, sometimes people want to return explicitly from somewhere
within the function:

def foo(x):
if x<0:
return
else:
whatever

--
Grant Edwards grante Yow! Two with FLUFFO,
at hold th' BEETS...side of
visi.com SOYETTES!
Jul 4 '06 #2

P: n/a
"yaru22" <ya****@gmail.comwrote:
>In one of the examples in the book I'm reading, it says:

def __init__(self):
...
...
...
return

It has nothing after "return". I expected it to have some number like 0
or 1.

What does it mean to have nothing after return?
It means "the function is over, go back to the caller now".
>Why do we even include "return" if we are not putting any value after?
If it is the last statement in the function, then you are right, it serves
no purpose. Some people include one for completeness -- a coding standard
that "all paths must have a return statement", for instance.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 4 '06 #3

P: n/a
"yaru22" wrote:
In one of the examples in the book I'm reading, it says:

def __init__(self):
...
...
...
return

It has nothing after "return". I expected it to have some number like 0 or 1.

What does it mean to have nothing after return?
"return" is the same thing as "return None"

http://pyref.infogami.com/return
Why do we even include "return" if we are not putting any value after?
a plain "return" at the end of a function is entirely optional:
>>def func1():
.... print
....
>>def func2():
.... print
.... return
....
>>def func3():
.... print
.... return None
....
>>import dis
dis.dis(func1)
2 0 PRINT_NEWLINE
1 LOAD_CONST 0 (None)
4 RETURN_VALUE
>>dis.dis(func2)
2 0 PRINT_NEWLINE
3 1 LOAD_CONST 0 (None)
4 RETURN_VALUE
>>dis.dis(func3)
2 0 PRINT_NEWLINE
3 1 LOAD_CONST 0 (None)
4 RETURN_VALUE

</F>

Jul 4 '06 #4

P: n/a

"yaru22" wrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
In one of the examples in the book I'm reading, it says:

def __init__(self):
...
return

It has nothing after "return". I expected it to have some number like 0
or 1. What does it mean to have nothing after return?
Yes, it has some special value. The value is said to be None.
Better to say -- the doc says:

"None
This type has a single value. There is a single object with this
value. This object is accessed through the built-in name None.
It is used to signify the absence of a value in many situations,
e.g., it is returned from functions that don't explicitly return
anything. Its truth value is false.
Why do we even include "return" if we are not putting any value after?
If you do not use return in your function, it is the same as if
you used return without an argument at the end of the body.
It will return None.

If you use return, the function returns immediately to the caller.
It need not to be the last command. You may want to return
earlier.

pepr
Jul 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.