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.

Edit
Report