a question about "return"

Y

yaru22

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
 
G

Grant Edwards

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
 
P

Petr Prikryl

in message
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
 
T

Tim Roberts

yaru22 said:
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.
 
F

Fredrik Lundh

yaru22 said:
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:
.... print
........ print
.... return
........ print
.... return None
.... 2 0 PRINT_NEWLINE
1 LOAD_CONST 0 (None)
4 RETURN_VALUE 2 0 PRINT_NEWLINE
3 1 LOAD_CONST 0 (None)
4 RETURN_VALUE 2 0 PRINT_NEWLINE
3 1 LOAD_CONST 0 (None)
4 RETURN_VALUE

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top