a question about "return"

Fredrik Lundh fredrik at pythonware.com
Tue Jul 4 03:21:41 EDT 2006


"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> 






More information about the Python-list mailing list