[Python-ideas] Replacing the if __name__ == "__main__" idiom (was Re: making a module callable)

Oscar Benjamin oscar.j.benjamin at gmail.com
Tue Nov 26 00:37:21 CET 2013


On 25 November 2013 22:29, Guido van Rossum <guido at python.org> wrote:
>> I guess what I don't like about is_main() is that it's a function call,
>> and is
>> two words separated by an underscore.  I have no technical arguments
>> against
>> it, just that to me it doesn't look as pretty.  And also, I guess, a
>> function
>> call seems a little more magical than checking an attribute value.  What
>> does
>> the function *do*?  OTOH, I guess a shadowed builtin variable is a little
>> magical too, but maybe a touch more transparent magic. ;)
>
>
> For all I care you can call it ismain().
>
> But it should be a function so that it's clear that the same function can
> return a different value in different contexts.

How is that clear? That's precisely what functions don't normally do
(in Python, maths, other programming languages...).

There already seems to be confusion around the magical super()
function precisely because it does different things in different
contexts and this is unexpected of something that uses function call
syntax.


Oscar


More information about the Python-ideas mailing list