Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"__main__":': 0.09; '"if': 0.09; '__name__': 0.09; 'convention,': 0.09; 'decorator': 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; 'def': 0.12; "'__main__':": 0.16; 'blocks': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'header:User-Agent:1': 0.23; 'requirement.': 0.24; 'header:X-Complaints-To:1': 0.27; 'chris': 0.29; 'statement': 0.30; 'along': 0.30; 'code': 0.31; 'lines': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'subject:some': 0.31; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'useful': 0.36; 'ahead': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'tell': 0.60; 'simply': 0.61; 'received:217': 0.63; 'guarantee': 0.63; 'can:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Vito De Tullio Subject: Re: How about some syntactic sugar for " __name__ == '__main__' "? Date: Sun, 16 Nov 2014 11:26:50 +0100 References: <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.com> <54673458$0$12999$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: 217.200.202.179 User-Agent: KNode/4.14.2 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416133623 news.xs4all.nl 2935 [2001:888:2000:d::a6]:33235 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80873 Steven D'Aprano wrote: > Chris Kaynor wrote: > >> I was thinking along the lines of replacing: >> >> if __name__ == "__main__": >> <<>> >> >> with >> >> @main >> def myFunction() >> <<<> >> >> Both blocks of code will be called at the same time. > > > You can't guarantee that, because you cannot tell ahead of time when the > "if __name__" statement will be run. It is *usually* at the end of the > file, but that's just the normal useful convention, it is not a hard > requirement. sure you can: simply the main decorator is just something like def main(myMainFunction): if myMainFunction.__module__ == '__main__': myMainFunction() return myMainFunction -- By ZeD