__main__ : What is this?

Peter Otten __peter__ at web.de
Sat Oct 20 03:32:37 EDT 2007


Steven D'Aprano wrote:

> On Sat, 20 Oct 2007 00:26:22 +0000, Matimus wrote:
> 
>> The common pattern:
>> 
>> if __name__ == "__main__":
>>   # do stuff
>> 
>> IMHO better written:
>> 
>> if "__main__" == __name__:
>>     # do stuff
> 
> Apart from looking weird, what's the difference?

In C this style is sometimes propagated as a means to avoid accidental
assignment:

if (main="main") {...} /* valid C, always true */
if ("main"=main) {...} /* syntax error */

In Python it would be cargo cult.

Peter



More information about the Python-list mailing list