[Python-ideas] breaking out of module execution

Ron Adam ron3200 at gmail.com
Wed Apr 25 22:27:33 CEST 2012

On 04/25/2012 02:31 PM, Georg Brandl wrote:
> On 25.04.2012 17:52, Ron Adam wrote:
>> 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.
> No, sorry, that's not a good equivalence.  It reinforces the impression some
> people have of "import" working like "#include" in C or (God forbid) "require"
> in PHP.

Not quite the same thing, but I see how you would think that from the way I 
wrote the example.

I didn't mean the file to be inserted, but instead as if it was written in 
the module statement body.  That is, if we even had a "module" keyword, 
which we don't. ;-)

The point is that a module contents execute from beginning to end to create 
a module, in the same way a class's contents execute from beginning to end 
to create a class.

There are also differences, such as where a module is stored and how it's 
contents are accessed, and so they are not the same thing. You can't just 
change a class into a module and vise-versa by just changing it's header or 
moving it's body into a separate file.

I was just trying to point out a module is closer to a class than it is to 
a function, and that is a good thing.  Allowing a return or break in a 
module could make things more confusing.  Also, by not allowing return or 
breaks, it catches errors were the indentation is lost in functions or 
methods quicker.


More information about the Python-ideas mailing list