Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.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.048 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; '"__main__":': 0.09; 'decorator': 0.09; 'subject:How': 0.10; 'def': 0.12; '11:32': 0.16; 'func': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'thu,': 0.19; '>>>': 0.22; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; '13,': 0.31; 'idea,': 0.31; 'omitted': 0.31; 'subject:some': 0.31; 'though.': 0.31; 'actual': 0.34; 'could': 0.34; 'received:google.com': 0.35; 'should': 0.36; 'wrong': 0.37; 'too': 0.37; 'being': 0.38; 'implement': 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; 'ethan': 0.84; 'furman': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=QGlsPSu7tV5GfsTw8tZwvOkELvAFXi3c88WSQcACWlM=; b=UivOi9V12Nt/+IMckXtIjP+DbRZ5HWJyM8TV+9YS1dsgqCsj3yEtjUWMt26Z0ySbYZ btnx6hnGL74gzb27j+C6DFcm7FxJ63s3fLJUWqWeLL8hRswjwFZk8G4g6hj2H2qIhS+q XmUitiiOeiEpVk1f20jaXpmT5jeSXCBj7SqB1oibPIEuQHVdMamrNwW8vFKjgs3LB3Oj nk88rC3wZHlbeOOBlQWugpkWN5rywHGSf2fJft4qMzgjLBbhjyfoPx/fw+3t6uQj6CI8 5izu8BjkcJUEQRFlFdHXhE0IFvxio8RBLsIB80mmw56wh3AHyM9OQXxfwDS0atoH+N8e dxoQ== X-Received: by 10.66.191.135 with SMTP id gy7mr5181781pac.95.1415910860299; Thu, 13 Nov 2014 12:34:20 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <5464F958.4070404@stoneleaf.us> References: <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.com> <5464F958.4070404@stoneleaf.us> From: Ian Kelly Date: Thu, 13 Nov 2014 13:33:40 -0700 Subject: Re: How about some syntactic sugar for " __name__ == '__main__' "? To: Python Content-Type: text/plain; charset=UTF-8 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415910868 news.xs4all.nl 2963 [2001:888:2000:d::a6]:51938 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80700 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.