Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'yet.': 0.04; 'classes,': 0.05; '"__main__":': 0.09; '*args,': 0.09; 'decorator': 0.09; 'global,': 0.09; 'imported': 0.09; 'preferable': 0.09; 'subject:How': 0.10; 'def': 0.12; "'__main__':": 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; 'defined,': 0.16; 'func': 0.16; 'script,': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'script.': 0.24; 'script': 0.25; 'define': 0.26; 'this:': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'message-id:@mail.gmail.com': 0.30; 'idea,': 0.31; 'invoke': 0.31; 'omitted': 0.31; 'subject:some': 0.31; 'though.': 0.31; "i'd": 0.34; 'could': 0.34; 'etc.)': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'module.': 0.36; 'should': 0.36; 'wrong': 0.37; 'easily': 0.37; 'being': 0.38; 'implement': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'easy': 0.60; 'manually': 0.60; 'within': 0.65; 'decorate': 0.84; 'idiom': 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=nRL2GWnBlLIoVmI8N6CwZh8tPX9H/tJVw14jq/nSGuE=; b=NfEtUVUyv9u02Wvi0e8X6h8Bv72hnn5UVRYNTQa6QowomAZ9gQUzlJnIV0jCoX6Cg6 qpyIu95vG53+u3oITiw7O6Nmb5I/vMN1YBQkQKoD02JvGcu2z+OHL0Qd8ENlJ8VInDbU E5Hnc2HfXLjtiqbt84eDSnJPKFFS5FIG53LdG9Uzt4k7JinmORgqKLjpp8lGCXta8ViY udx5xSqGDeh/F/8TB+AwHVwP7ZkRton0x6RLbeTZllE+8QcE7Kb6eZGSFoEjdihDI9Ub jIC/H89lkCPCRQA5O5zPbNzjl+9tPlZT/8FfFdHm/8VnVjXQmConv89RpVq/Bxr/2acD 38vQ== X-Received: by 10.68.93.1 with SMTP id cq1mr22072607pbb.145.1415829157446; Wed, 12 Nov 2014 13:52:37 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.com> From: Ian Kelly Date: Wed, 12 Nov 2014 14:51:57 -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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415829166 news.xs4all.nl 2947 [2001:888:2000:d::a6]:59316 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80634 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. > > 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. This also calls the function before it's been assigned to the global, which would prevent recursive calls of the main function. Instead of a decorator, I'd prefer to just have this: def main(func, *args, **kwargs): if func.__module__ == '__main__': func(*args, **kwargs) And then I can easily invoke it wherever I want in the main script.