Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4a.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '"__main__":': 0.09; '"if': 0.09; '__name__': 0.09; 'decorator': 0.09; 'executed': 0.09; 'executes': 0.09; 'implemented.': 0.09; 'imported': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'decade,': 0.16; 'exactly,': 0.16; 'func': 0.16; 'hides': 0.16; 'how,': 0.16; 'merely': 0.16; 'namespace,': 0.16; 'pythonic': 0.16; 'script,': 0.16; 'such,': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'wed,': 0.18; 'module': 0.19; "python's": 0.19; 'programming': 0.22; 'email addr:gmail.com>': 0.22; 'to:name:python- list@python.org': 0.22; 'string,': 0.24; "haven't": 0.24; "i've": 0.25; 'script': 0.25; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; "skip:' 10": 0.31; 'idea,': 0.31; 'omitted': 0.31; 'proposing': 0.31; 'subject:some': 0.31; 'worked': 0.33; 'received:74.125.82': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; '8bit%:9': 0.36; 'module.': 0.36; 'thanks': 0.36; 'should': 0.36; 'too': 0.37; 'two': 0.37; 'being': 0.38; 'implement': 0.38; 'skip:& 10': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'skip:& 20': 0.39; 'received:74.125': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'how': 0.40; 'easy': 0.60; 'manually': 0.60; 'details.': 0.61; 'john': 0.61; 'name': 0.63; 'myself': 0.63; 'determine': 0.67; 'bothered': 0.68; 'deeply': 0.69; 'decorate': 0.84; 'idiom': 0.84; 'joel': 0.91; 'students,': 0.95; 'taught': 0.96; 'invite': 0.98 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:content-type; bh=se+wSAn/Oa3sULQYNH8JhpL2/YcFgsYIQ+041f7byF8=; b=T2qoOsMCzsnfwgbjEABXNEaxfOSH94zrOkjp4rRj6pa4pjeKODKv08uRb0Wl7NwNdv O0DxKTHmU6W1h7rqDFpE4C/h/d5nHYYmx+CGrsgrgVacCRm8nLvSAzOBPIAJGkRAjQqX ePa3zBWmRdw9BZ53YepTeL3RvUkHSkoj8JnrKndTzvCLZfDLTC79TLC516b5wgWD4OrT SSDOyHJ+/MukBH8xJLA1cjEOblwMFrJb6JlBpadRALLOrQb7D9cRHjjaHTxjODouVY4d KfHKPo8lFKYHQEIDK8wyBlvxhYTOVS+bblcRUoPWz6O/v9o3SDHYZlJqbRrJmQj675VE LQDA== X-Gm-Message-State: ALoCoQk3kLaM3ITUQgyopIqJWXig6ITCr4uAW5PabPwK+cPP1ioDasFm3oyhG58icgQ/4PSFmfii X-Received: by 10.181.12.6 with SMTP id em6mr7429388wid.24.1415828051329; Wed, 12 Nov 2014 13:34:11 -0800 (PST) MIME-Version: 1.0 In-Reply-To: References: <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.com> From: Chris Kaynor Date: Wed, 12 Nov 2014 13:33:51 -0800 Subject: Re: How about some syntactic sugar for " __name__ == '__main__' "? To: "python-list@python.org" Content-Type: multipart/alternative; boundary=f46d043c7ede101b8f0507b0281a 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: 91 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415828053 news.xs4all.nl 2844 [2001:888:2000:d::a6]:46615 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 8518 X-Received-Body-CRC: 3002722040 Xref: csiph.com comp.lang.python:80631 --f46d043c7ede101b8f0507b0281a Content-Type: text/plain; charset=UTF-8 On Wed, Nov 12, 2014 at 1:07 PM, Joel Goldstick wrote: > On Wed, Nov 12, 2014 at 4:02 PM, John Ladasky > wrote: > > I have taught Python to several students over the past few years. As I > have worked with my students, I find myself bothered by the programming > idiom that we use to determine whether a module is being executed or merely > imported: > > > > "if __name__ == '__main__':" > > > > The use of two dunder tokens -- one as a name in a namespace, and the > other as a string, is intimidating. It exposes too much of Python's guts. > As such, I think that it is less Pythonic than we might want. Myself, I've > been programming in Python for a decade, and I still haven't dug very > deeply into what exactly __name__ means or does. > > > > I would like to start a discussion about whether Python should include a > function which executes this evaluation, and hides all of the unfriendly > dunderish details. And if that's a good idea, I would invite a discussion > of how, exactly, it should be implemented. I'm nowhere near proposing a > PEP, but that may come later. > > > > Thanks for your thoughts. > > How about a decorator? > 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. --f46d043c7ede101b8f0507b0281a Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Wed, Nov 12, 2014 at 1:07 PM, Joel Goldstick <joel.goldstick@gmail.com> wrote:
On Wed, Nov 12, 2014 at 4:02 PM, John Ladasky
<john_ladasky@sbcglobal.ne= t> wrote:
> I have taught Python to several students over the past few years.=C2= =A0 As I have worked with my students, I find myself bothered by the progra= mming idiom that we use to determine whether a module is being executed or = merely imported:
>
>=C2=A0 =C2=A0"if __name__ =3D=3D '__main__':"
>
> The use of two dunder tokens -- one as a name in a namespace, and the = other as a string, is intimidating.=C2=A0 It exposes too much of Python'= ;s guts.=C2=A0 As such, I think that it is less Pythonic than we might want= .=C2=A0 Myself, I've been programming in Python for a decade, and I sti= ll haven't dug very deeply into what exactly __name__ means or does. >
> I would like to start a discussion about whether Python should include= a function which executes this evaluation, and hides all of the unfriendly= dunderish details.=C2=A0 And if that's a good idea, I would invite a d= iscussion of how, exactly, it should be implemented.=C2=A0 I'm nowhere = near proposing a PEP, but that may come later.
>
> Thanks for your thoughts.

How about a decorator?

A de= corator is an interesting idea, and should be easy to implement (only light= ly tested):

def main(func):
=C2=A0 =C2= =A0 if func.__module__ =3D=3D "__main__":
=C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 func()
=C2=A0 =C2=A0 return func # T= he 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.
--f46d043c7ede101b8f0507b0281a--