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

Ethan Furman ethan at stoneleaf.us
Tue Nov 26 00:46:54 CET 2013


On 11/25/2013 03:37 PM, Oscar Benjamin wrote:
> 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...).

Any function which deals with the outside world returns different values based on context (aka the real world):

   - date
   - time
   - disk
   - geo-location
   - locals()
   - globals()
   - vars()

etcetera, etcetera, and so-forth.

--
~Ethan~


More information about the Python-ideas mailing list