Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #80642

Re: How about some syntactic sugar for " __name__ == '__main__' "?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ckaynor@zindagigames.com>
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; 'interpreter': 0.05; '"__main__":': 0.09; '*args,': 0.09; '__name__': 0.09; 'decorator': 0.09; 'global,': 0.09; 'imported': 0.09; 'namespace': 0.09; 'preferable': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'def': 0.12; "'__main__':": 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; 'blocks': 0.16; 'called.': 0.16; 'defined,': 0.16; 'elsewhere,': 0.16; 'func': 0.16; 'script,': 0.16; '\xc2\xa0if': 0.16; 'prevent': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'issue,': 0.24; 'script.': 0.24; 'skip': 0.24; 'cc:2**0': 0.24; 'script': 0.25; '&gt;': 0.26; 'define': 0.26; 'this:': 0.26; 'defined': 0.27; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'chris': 0.29; '(like': 0.30; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; 'code': 0.31; 'lines': 0.31; 'block,': 0.31; 'idea,': 0.31; 'invoke': 0.31; 'omitted': 0.31; 'subject:some': 0.31; 'though.': 0.31; 'another': 0.32; 'plain': 0.33; "i'd": 0.34; 'received:74.125.82': 0.34; 'could': 0.34; 'etc.)': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; '8bit%:9': 0.36; 'module.': 0.36; 'should': 0.36; 'wrong': 0.37; 'easily': 0.37; 'being': 0.38; 'implement': 0.38; 'skip:& 10': 0.38; 'nov': 0.38; 'pm,': 0.38; 'that,': 0.38; 'skip:& 20': 0.39; 'received:74.125': 0.39; 'does': 0.39; '12,': 0.39; 'extremely': 0.39; 'called': 0.40; 'easy': 0.60; 'ian': 0.60; 'manually': 0.60; 'simple,': 0.60; 'mentioned': 0.61; 'such': 0.63; 'to:addr:gmail.com': 0.65; 'within': 0.65; 'skip:m 50': 0.68; 'decorate': 0.84; 'ian,': 0.84; 'idiom': 0.84; 'absolutely': 0.87; '8bit%:33': 0.91
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=p2rxPB8tmXEzxQoMQ24JcRfTPrLQZxO0j1tu3w25KB0=; b=NbdUYf3Qqc/Pppen+3d5gkgGAkIuPTZxnioFEjf3ZZbrvvhSFuLST5r3jLpsTKVJD7 v+voFA36ebPq8PSCB1MwdT+s2e2RxOH1QWtGrNCmn++zMTS/QI+kwgynXXJjOL0+9TuW gvdVCsEGNM48A6uRnY32FaZIO19kigtMJ5lnGkWCmm//OjuCiP19oU9kYo4xyxObWO33 2a5ka4u9RNsRUHPt9/0tkpEjGBQeI42YrPsAq0HfJ2dOBPFkVfSq6L5lRJabLkufTZew opz4f7ryF/xbwxg2iBgcsXYpWt3CC2sbkMY/sOHqdhH/VccvFOEchErRsYuof6F4GfTm UChg==
X-Gm-Message-State ALoCoQlxXRru8ADrkVBT+xtrnarUyhUucO8wi4cVKAVHdH7XS4U3Nvcdt8VL5mHF+F20GqQPJBnZ
X-Received by 10.180.9.196 with SMTP id c4mr54118260wib.66.1415830169813; Wed, 12 Nov 2014 14:09:29 -0800 (PST)
MIME-Version 1.0
In-Reply-To <CALwzid=i6vKUnQU1pfO3qs8H5u+_J4ff_XdkQUxt4ajN5JNYfQ@mail.gmail.com>
References <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.com> <CAPM-O+yukMKq8m1Udcw23=kgPc6zvncfS1ohPcyme_4RXMTATg@mail.gmail.com> <CALvWhxuo8EB_FqkeUXe9nRj1CGFxrVtzw76afc21cB5=D4aZwg@mail.gmail.com> <CALwzid=i6vKUnQU1pfO3qs8H5u+_J4ff_XdkQUxt4ajN5JNYfQ@mail.gmail.com>
From Chris Kaynor <ckaynor@zindagigames.com>
Date Wed, 12 Nov 2014 14:09:09 -0800
Subject Re: How about some syntactic sugar for " __name__ == '__main__' "?
To Ian Kelly <ian.g.kelly@gmail.com>
Content-Type multipart/alternative; boundary=001a11c24302559c8e0507b0a6b5
Cc Python <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.15748.1415830171.18130.python-list@python.org> (permalink)
Lines 151
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1415830171 news.xs4all.nl 2879 [2001:888:2000:d::a6]:52234
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:80642

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Wed, Nov 12, 2014 at 1:51 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:

> On Wed, Nov 12, 2014 at 2:33 PM, Chris Kaynor <ckaynor@zindagigames.com>
> 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.
>
>
I was thinking along the lines of replacing:

if __name__ == "__main__":
    <<<block of code>>>

with

@main
def myFunction()
    <<<<block of code>>

Both blocks of code will be called at the same time.


> 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.



> On Wed, Nov 12, 2014 at 1:55 PM, Skip Montanaro <skip.montanaro@gmail.com>
 wrote:
>
> This won't work (I don't think) if you want to call the "main"

function from another place (like the interpreter prompt).


With the plain if block, you absolutely cannot call it elsewhere, without
wrapping it in a function anyways.

There is the issue, as mentioned by Ian, that the function will not be in
the module namespace at the time it is called. That does block it, however
it is also easy to work around: make the main function extremely simple,
such as just calling another function.

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How about some syntactic sugar for " __name__ == '__main__' "? John Ladasky <john_ladasky@sbcglobal.net> - 2014-11-12 13:02 -0800
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-12 16:07 -0500
  Re: How about some syntactic sugar for " __name__ == '__main__' "? John Ladasky <john_ladasky@sbcglobal.net> - 2014-11-12 13:17 -0800
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-11-12 13:19 -0800
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Kaynor <ckaynor@zindagigames.com> - 2014-11-12 13:33 -0800
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Marko Rauhamaa <marko@pacujo.net> - 2014-11-12 23:52 +0200
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-12 14:51 -0700
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-12 15:55 -0600
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Kaynor <ckaynor@zindagigames.com> - 2014-11-12 14:09 -0800
    Re: How about some syntactic sugar for " __name__ == '__main__' "? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-15 22:09 +1100
      Re: How about some syntactic sugar for " __name__ == '__main__' "? Marko Rauhamaa <marko@pacujo.net> - 2014-11-15 13:54 +0200
      Re: How about some syntactic sugar for " __name__ == '__main__' "? Vito De Tullio <vito.detullio@gmail.com> - 2014-11-16 11:26 +0100
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-12 15:19 -0700
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Terry Reedy <tjreedy@udel.edu> - 2014-11-12 18:19 -0500
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Angelico <rosuav@gmail.com> - 2014-11-13 10:26 +1100
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Terry Reedy <tjreedy@udel.edu> - 2014-11-12 19:35 -0500
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Angelico <rosuav@gmail.com> - 2014-11-13 11:54 +1100
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-11-13 10:32 -0800
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-13 13:33 -0700
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-11-13 12:43 -0800
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-13 14:44 -0600
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Skip Montanaro <skip.montanaro@gmail.com> - 2014-11-13 14:53 -0600
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-13 15:48 -0700
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-13 15:56 -0700
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-15 21:52 +1100
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Vito De Tullio <vito.detullio@gmail.com> - 2014-11-16 11:39 +0100
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ian Kelly <ian.g.kelly@gmail.com> - 2014-11-16 10:15 -0700
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-12-01 15:19 -0800
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-12-01 16:45 -0800
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Chris Angelico <rosuav@gmail.com> - 2014-12-02 12:15 +1100
  Re: How about some syntactic sugar for " __name__ == '__main__' "? Ethan Furman <ethan@stoneleaf.us> - 2014-12-02 02:17 -0800

csiph-web