Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Marko Rauhamaa Newsgroups: comp.lang.python Subject: Re: How about some syntactic sugar for " __name__ == '__main__' "? Date: Wed, 12 Nov 2014 23:52:41 +0200 Organization: A noiseless patient Spider Lines: 40 Message-ID: <8761ekqe6u.fsf@elektro.pacujo.net> References: <71b9a111-283f-4006-bc9d-e35782d9888c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="ff5cf27ef3d5b31f034d3b72bdc27a41"; logging-data="32342"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/usltWFuP6o/gAlbaVkpHx" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:CyAG8D7Y7fRD3YRYJz5Sd6xiJtc= sha1:/rrb6pLTgHbsWJ0rLwfmNNBb7G8= Xref: csiph.com comp.lang.python:80633 John Ladasky : > 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__':" I find the idiom cute and loveably silly. A typing tongue twister of sorts. That boilerplate is far less cumbersome than, say, in Java. > 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. The idiom allows you to include a main function in auxiliary modules. When imported, the module acts as a library. When executed, it acts as a command. I have done this a couple of times IRL. > 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. Probably not. Idioms are important in that they are immediately spotted by a casual reader of the code. However ugly you consider those two lines, it will not waste a measurable amount of your precious time to begin your brand new project by typing: import sys def main(): pass if __name__ == "__main__": main() Yes, I always type those in again. I don't copy them over as I do, say, Java main programs or static HTML pages. Marko