Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.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; 'handler': 0.05; 'skip:l 60': 0.07; '"__main__":': 0.09; 'decorator': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'function,': 0.09; 'message- id:@stoneleaf.us': 0.09; 'pgp': 0.09; 'skip:o 60': 0.09; 'skip:t 60': 0.09; '~ethan~': 0.09; 'subject:How': 0.10; 'def': 0.12; '(gnu/linux)': 0.16; '-----begin': 0.16; '-----end': 0.16; '11:32': 0.16; 'func': 0.16; 'gnupg': 0.16; 'hash:': 0.16; 'sha1': 0.16; 'skip:/ 60': 0.16; 'skip:j 60': 0.16; 'skip:q 60': 0.16; 'stuff!': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'message-----': 0.19; 'thu,': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'decorators': 0.24; 'skip:n 60': 0.24; 'signed': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; '13,': 0.31; '>>>>': 0.31; 'idea,': 0.31; 'omitted': 0.31; 'skip:i 60': 0.31; 'skip:m 60': 0.31; 'skip:r 60': 0.31; 'subject:some': 0.31; 'though.': 0.31; 'run': 0.32; 'actual': 0.34; 'could': 0.34; 'version:': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'wrong': 0.37; 'too': 0.37; 'being': 0.38; 'implement': 0.38; 'checks': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'easy': 0.60; 'ian': 0.60; 'manually': 0.60; 'received:173': 0.61; 'name': 0.63; 'such': 0.63; 'ethan': 0.84; 'furman': 0.84; 'niche': 0.91 Date: Thu, 13 Nov 2014 12:43:15 -0800 From: Ethan Furman User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How about some syntactic sugar for " __name__ == '__main__' "? References: <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.com> <5464F958.4070404@stoneleaf.us> In-Reply-To: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit 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: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415911400 news.xs4all.nl 2951 [2001:888:2000:d::a6]:56612 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80701 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 11/13/2014 12:33 PM, Ian Kelly wrote: > On Thu, Nov 13, 2014 at 11:32 AM, Ethan Furman wrote: >> On 11/12/2014 01:51 PM, Ian Kelly wrote: >>> >>> On Wed, Nov 12, 2014 at 2:33 PM, Chris Kaynor wrote: >>>> >>>> A decorator is an interesting idea, and should be easy to implement (only lightly tested): >>>> >>>> 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. >>> >>> This calls it at the wrong time, though. [...] >> >> One decorator that won't call too early is atexit.register(). > > Nice. That feels like an abuse, though. The actual program won't be able to register its own atexit handlers, > because the program will already be exiting, and other things decorated with atexit.register might actually be > called before the main function. It's definitely a niche use-case -- such as a cli-helper lib: import said lib, and when it's decorators are used set the atexit handler to call the lib's __main__ function, which checks that the module name of the decorated function is '__main__', and if it is, run stuff! Hmmm... hopefully that wasn't difficult to explain. ;) - -- ~Ethan~ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.11 (GNU/Linux) iQIcBAEBAgAGBQJUZRfjAAoJENZ7D1rrH75Nx4IP/3tuR4JhVK6Acv04+DiHm32H BHGmBRZGtXQrHGEBcv8BlvUiztmrHqjV+Lkm78zKF3n9R8Ta9qTeSjqIsjRWUMtR JVraCSH6usUwmcGITXIYKQHdXynl+ylu9p8Hr3NT2aNoICqowVGSvK3Ie1NmJuf3 lJcl8tXiXgUrGCwBwEgdrBKTdaATe4QT9XFQJx1QbXpF3qT1Za5hPYthY3fH/Pd9 Nl9NHyA6F5x4sSO7itD23UtUpnRBHWl7blwsKkBi7ClfacxJMrYjFAMUaxUaiTFF /bygveskmMAZw87ISLtjkmOcKtsi0i2BQSQEjpBDZTiveCD/wyDDhJ+5pmZKzll0 Q+pISt4jG9hkArd+JCCxPuTCCo2xm+cMIB4/oSeONd760u6vURLPUNZ5tmNsRkiZ o0/EwyRhWPZotiLoyi7kDNyfpj/BSKV0A6Ph+M40UXOkTZFUf0E84OFEpB359MTO rIvHDpd6Tzch2Dliuj6UKZ1OOygIZauv2ebmEBHHNDMdsVbhdtTElS1Rh2JtDVyZ tUd67KTmE9CwOPcFIbYKHXGIf4FWDAgeaz9x8RY4UUPaDRlyG1eqSxx8Vob2AG9c MEawmA48JCj52ZeICQf6nnIFdIowFkV7QssUgte3cVfbcMBYnPx/nuJWWMjULo77 WrIUpisDf/juvXn4VtuO =oczI -----END PGP SIGNATURE-----