Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: why is `return` not allowed in a module

If you look at the Python bytecode compiled from modules, you'll see at the end:

10 LOAD_CONST               0 (None)
13 RETURN_VALUE        

I.e. a return is perfectly valid at the root level of a module. However, if you try to use it in the source code, you get:

SyntaxError: 'return' outside function

Why?


This is of course a language design decision. But why was it taken? A return is often quite useful in a module. E.g. I sometimes want to write this code at the beginning of a module:

import sys
if sys.platform != "linux2":
    print "your platform is not yet supported"
    # define some stubs
    def foo(): pass
    def bar(): pass
    return

And I don't want to raise an exception there (because that would result in the module not loading which I don't want).


Actually, I hacked some code together which can manipulate the Python bytecode on the fly and just do the return (or in my case jump to the end where the return is done). This works. The only reason it is CPython only is because there is no standard way to manipulate a code object.

Here is the code.


For possible answers: I would prefer real evidence, some past statements from Guido van Rossum or so about this, not so much just any random discussion. I of course can this of both advantages (as presented already) and disadvantages myself but I don't really see the big reason against it.

like image 537
Albert Avatar asked Sep 07 '12 04:09

Albert


People also ask

How does return work in Python?

The Python return statement is a special statement that you can use inside a function or method to send the function's result back to the caller. A return statement consists of the return keyword followed by an optional return value. The return value of a Python function can be any Python object.

How do you return an object in Python?

The return statement makes a python function to exit and hand back a value to its caller. The objective of functions in general is to take in inputs and return something. A return statement, once executed, immediately halts execution of a function, even if it is not the last statement in the function.

Is return and return none the same?

As other have answered, the result is exactly the same, None is returned in all cases. The difference is stylistic, but please note that PEP8 requires the use to be consistent: Be consistent in return statements. Either all return statements in a function should return an expression, or none of them should.

Can a Python function return nothing?

In Python, it is possible to compose a function without a return statement. Functions like this are called void, and they return None, Python's special object for "nothing".


1 Answers

I would guess that this is a decision on the part of the python devs because it forces the user of the module to deal with the fact that their system isn't supported (rather than getting the developers of the module to guess what should happen). Imagine their surprise when they run the code on a windows machine and all of a sudden module.foo() doesn't work as they expected. (Your warning print statement could be completely buried in all sorts of other garbage that might get dumped to stdout).

I think that the most idomatic way to handle this is to raise an ImportError or NotImplementedError. Then the user can decide if they can live without this module and proceed, (by catching the exceptions), or if their program should crash and burn right there.

like image 186
mgilson Avatar answered Sep 26 '22 03:09

mgilson