Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'subject:module': 0.04; 'executing': 0.05; 'francis': 0.07; 'indentation': 0.07; "shouldn't": 0.07; 'statements,': 0.07; '__name__': 0.09; 'either.': 0.09; 'erik': 0.09; 'exceptions': 0.09; 'executed': 0.09; 'object.': 0.09; 'return,': 0.09; 'statement.': 0.09; 'pm,': 0.10; 'exception': 0.12; 'wrote:': 0.14; '"__main__":': 0.16; '[1].': 0.16; 'born.': 0.16; 'definition.': 0.16; 'executed.': 0.16; 'furman': 0.16; 'if-else': 0.16; 'remember.': 0.16; 'case.': 0.16; 'cc:addr:python-list': 0.17; 'compiled': 0.17; 'tue,': 0.17; 'of.': 0.19; 'once,': 0.19; 'header:In-Reply-To:1': 0.21; 'cc:2**0': 0.22; 'stuff': 0.22; 'cc:no real name:2**0': 0.23; 'body.': 0.23; 'happen.': 0.23; 'module,': 0.23; 'statement,': 0.23; "doesn't": 0.25; 'certainly': 0.25; 'function': 0.25; 'expect': 0.25; 'modules': 0.26; 'statement': 0.26; 'url:mailman': 0.26; 'not.': 0.26; 'object': 0.26; 'script': 0.27; 'example': 0.27; 'message- id:@mail.gmail.com': 0.28; 'raise': 0.28; 'import': 0.29; 'bound': 0.29; 'work:': 0.29; 'cc:addr:python.org': 0.30; 'url:listinfo': 0.30; 'fact': 0.30; 'module': 0.30; 'putting': 0.30; 'received:209.85.215': 0.30; 'received:209.85.215.46': 0.30; 'received:mail-ew0-f46.google.com': 0.30; 'it.': 0.31; 'agree': 0.32; 'execution': 0.32; 'does': 0.33; 'break': 0.33; 'handling': 0.33; 'too': 0.33; 'rather': 0.34; '...': 0.34; 'decide': 0.34; 'file': 0.34; 'however,': 0.34; '[1]': 0.34; 'expensive': 0.34; 'there': 0.35; 'skip:" 10': 0.35; 'ca,': 0.35; 'using': 0.35; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'checks': 0.37; 'instead.': 0.37; 'keywords': 0.37; 'ways': 0.37; 'pretty': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'run': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'should': 0.39; 'perhaps': 0.39; 'received:209': 0.39; 'either': 0.39; 'add': 0.39; 'really': 0.40; 'more': 0.60; 'kind': 0.60; 'hope': 0.60; 'waiting': 0.61; 'body': 0.61; 'levels': 0.63; 'harder': 0.65; 'special': 0.66; 'kept': 0.67; 'flow': 0.68; 'usa': 0.69; 'alternative': 0.71; 'apologize': 0.72; 'issued': 0.73; 'louis': 0.73; 'placed': 0.76; 'abnormal': 0.84; 'execution,': 0.91; 'follow.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=84u6AKX9IlEzFxVCBovhyqm/4cqRRFMPqyqDYngvqO4=; b=VOthq2FCjhR3dE8f6F6r/JaCqx0X778VsKco3pJ7fAD8aR4h2EFLz+czfnZ7ZUT1sH aRm09tCkCJp0r1NsJfMzO3hLoF3/UkbdaARgEXoHEPF8a6RyRYiSE47pMtD7mZJhFsRw MXgOBLXKTn9GaBrSKio0d1qHKzcpn3LgqFwBA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=w7Fi2bkfjVpOZ5QO59zLvxGuReWDXBcx4XxVjWuTpGDDdA6ttzZ2ogE2gHIJ4XJoCn pKqWZVIUMlgt4ewB32gynRyzw0mdpat6Ae0wuZm5xP01C+yLRDnZj9Cbri4JjXcHYQhX GgkBmAW3ie0AWw8qLtdXEZs0MdWje90kubfIg= MIME-Version: 1.0 In-Reply-To: References: <4DF7E75E.9000907@mrabarnett.plus.com> Date: Tue, 14 Jun 2011 18:51:33 -0600 Subject: Re: break in a module From: Eric Snow To: Erik Max Francis Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 77 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308099095 news.xs4all.nl 49041 [::ffff:82.94.164.166]:53900 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:7657 On Tue, Jun 14, 2011 at 5:51 PM, Erik Max Francis wrote: > Ethan Furman wrote: >> >> To me, too -- too bad it doesn't work: >> >> c:\temp>\python32\python early_abort.py >> =A0File "early_abort.py", line 7 >> =A0 =A0return >> =A0 =A0 =A0 ^ >> SyntaxError: 'return' outside function > > Nor should it. =A0There's nothing to return out of. > Perhaps we have a misunderstanding then. The contents of a module file are the body of the module definition. Like the body of any other complex statement, that body is going to get executed [1]. Some of the complex statements have keywords that let you break out of that execution, like break and continue in loops. Some do not. However, there is most certainly something out of which to return, the execution of the module body. That fact that the functionality is not there does not mean it has to stay that way. It may just be that no one has thought to add it. I don't agree that it's a bad idea. I have a use case. The alternative is unappealing to me. That's how new features are born. I apologize if my example was unclear. I kept it pretty simple. I expect using __main__ was misleading. However, this is by no means the only use case. In general it would be nice to do some checks up front and decide whether or not to continue executing the module, rather than waiting until the end to decide: if condition_1: ... return if condition_2: ... return # now do my expensive module stuff # finally handle being run as a script if __name__ =3D=3D "__main__": ... The only ways that I know of to accomplish this currently is either by putting everything inside if-else blocks, or raise some kind of ImportBreak exception and catch it in an import hook. I would rather not use either one. The more levels of indentation in a module, the harder it is to follow. And exceptions really should not be involved in execution flow control, but in the handling of abnormal situations instead. Considering that other complex statements have special flow control statements, I don't see why modules shouldn't either. -eric [1] During import the module gets compiled and the result is exec'ed in the context of the __dict__ of a new ModuleType object. That module object is then placed in sys.modules and bound to the name you have in the import statement in the module from which you issued that statement. Remember, the module is executed once, when the import statement is executed. That is when the module flow control would happen. > -- > Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/ > =A0San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis > =A0There is _never_ no hope left. Remember. > =A0 -- Louis Wu > -- > http://mail.python.org/mailman/listinfo/python-list >