[Python-ideas] breaking out of module execution
Terry Reedy
tjreedy at udel.edu
Wed Apr 25 23:51:41 CEST 2012
On 4/25/2012 4:27 PM, Ron Adam wrote:
> 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.
Each should only be executed once, and this is enforced for modules by
the sys.modules cache.
> Allowing a return or break in a
> module could make things more confusing.
Agreed.
It is not actually necessary for functions to have an explicit return
statement. I believe that there are languages that define the function
return value as the last value assigned to the function name.
def fact(n):
if n > 1: fact = n*fact(n-1)
else: fact = 1
(This is pretty close to how mathematicians might write the definition.
It does, however, require special-casing the function name on the left
of '='.)
'return' is, however, useful for returning out of more deeply nested
constructs, especially loops, without setting flag variables or having
multi-level breaks. This consideration does not really apply to the use
case for module return.
A virtue of defining everything in Python and then trying to import the
accelerated override is that different implementations and versions
thereof can use the same file even though they accelerate different
parts and amounts (even none) of the module.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list