Path: csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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; 'yet.': 0.04; 'classes,': 0.05; '"__main__":': 0.09; 'imported': 0.09; 'preferable': 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; 'thread': 0.14; "'__main__':": 0.16; 'defined,': 0.16; 'func': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'script,': 0.16; 'thread,': 0.16; 'time"': 0.16; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; 'load': 0.23; 'header:User-Agent:1': 0.23; 'script': 0.25; 'define': 0.26; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'omitted': 0.31; 'subject:some': 0.31; 'though.': 0.31; 'could': 0.34; 'something': 0.35; 'etc.)': 0.35; 'but': 0.35; 'module.': 0.36; 'wrong': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'ian': 0.60; 'manually': 0.60; 'received:217': 0.63; 'choose': 0.64; 'within': 0.65; 'decorate': 0.84; 'idiom': 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:39:34 +0100 References: <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416134390 news.xs4all.nl 2871 [2001:888:2000:d::a6]:36084 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80874 Ian Kelly wrote: >> def main(func): >> if func.__module__ == "__main__": >> func() >> return func # The return could be omitted to block the function from >> being manually called after import. >> >> Just decorate the "main" function of the script with that, and it will be >> automatically called when ran as a script, but not when imported as a >> module. > > This calls it at the wrong time, though. Typically the way this idiom > is used is that you define everything you need (functions, classes, > etc.) within the main script, and then you call the main function. > This would call the main function at the time it's defined, when other > things in the main script may not have been defined yet. One could > place the main function last, but it would be preferable not to be > forced. for the "right time" you can choose to spin a thread and wait to the end of the load of the module something like from threading import Thread, current_thread def run_func(func, module_thread): module_thread.join() func() def main(func): if func.__module__ == '__main__': Thread(target=run_func, args=[func, current_thread()]).start() return func -- By ZeD