Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'languages,': 0.04; 'explicitly': 0.05; 'explicit': 0.07; '"if': 0.09; "'return": 0.09; '__name__': 0.09; 'function:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'jan': 0.12; "'__main__':": 0.16; '(which,': 0.16; 'invocation': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'simplest': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'thu,': 0.19; 'written': 0.21; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'code': 0.31; '13,': 0.31; 'implicit': 0.31; 'subject:some': 0.31; 'could': 0.34; 'common': 0.35; 'problem.': 0.35; 'but': 0.35; 'consistent': 0.36; 'should': 0.36; 'ends': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'such': 0.63; 'become': 0.64; 'more': 0.64; 'conflicts': 0.84; 'end.': 0.84; 'received:fios.verizon.net': 0.84; 'safer': 0.84; 'magical': 0.91; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: How about some syntactic sugar for " __name__ == '__main__' "? Date: Wed, 12 Nov 2014 19:35:59 -0500 References: <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415838984 news.xs4all.nl 2849 [2001:888:2000:d::a6]:41452 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80664 On 11/12/2014 6:26 PM, Chris Angelico wrote: > On Thu, Nov 13, 2014 at 10:19 AM, Terry Reedy wrote: >> Functions have an implicit 'return None' at the end (which, in CPython, >> become an explicit pair of bytecodes, even when the function already ends >> with return something'. The simplest proposal is that modules have an >> implicit "if __name__ == '__main__': main()" at the end. I think this would >> not have to be added to the bytecode. >> >> This magical invocation mimics C and some other languages, and I think it >> works well. > > Yes, but it conflicts with the existing and common usage of having > that explicitly in the code. Yeh, calling main twice could be a problem. > Safer - and more in line with the way > other such functions are written - would be a dunder function: > > if __name__ == '__main__': __main__() I presume you mean that calling __main__ implicitly would be both consistent and safer. No code should be using that now. -- Terry Jan Reedy