a question about "return"

Petr Prikryl prikryl at skil.cz
Tue Jul 4 01:52:03 EDT 2006


"yaru22" wrote in message
news:1151982728.154138.20310 at m79g2000cwm.googlegroups.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





More information about the Python-list mailing list