[Python-ideas] breaking out of module execution

Ron Adam ron3200 at gmail.com
Wed Apr 25 17:52:49 CEST 2012



On 04/25/2012 02:58 AM, Greg Ewing wrote:
> Nick Coghlan wrote:
>> It isn't quite as simple as
>> just deleting those lines though, since we likely still wouldn't want
>> to allow return statements in class bodies.
>
> I'm sure there's someone out there with a twisted enough
> mind to think of a use for that...

Currently return isn't allowed in class bodies defined inside functons.  So 
it probably won't work in top level either.

>>> def foo():
  ...    class A:
  ...       return
  ...
    File "<stdin>", line 3
  SyntaxError: 'return' outside function


As far as the feature goes, it wouldn't be consistent with class behaviour 
unless you allow the returns in class's to work too.

Think of modules as a type of class where ...

   import module

is equivalent to ...

   module module_name:
       <module file contents here>

Like classes the module body would execute to define the module, and return 
inside the module body would be a syntax error.

Of course since modules aren't specifically defined in this way, there is 
the option to not follow that consultancy.

Cheers,
    Ron







Ron






More information about the Python-ideas mailing list