Who else hates 'if __Name__ == "__main__"'?

Who else hates 'if __Name__ == "__main__"'?

Postby LjSpike » Sat Jan 09, 2016 7:39 pm

Who else really hates that bit of code:
Code: Select all
if __Name__ == "__main__"

I mean, after a bit of looking about I found out what its for, but it's not really self explanatory, plus whats up with the need for __ at the beginning and end of that. Especially when it comes to the string which __name__ could be. Why didn't python come up with something like:
Code: Select all
if ifProgramMain == True
Last edited by micseydel on Sat Jan 09, 2016 8:20 pm, edited 1 time in total.
Reason: First-ish post lock.
In order to understand recursion, one must first understand recursion.
LjSpike
 
Posts: 9
Joined: Sat Jan 09, 2016 3:34 pm
Location: A dark basement, where the only light source is that of numerous monitors

Re: Who else hates 'if __Name__ == "__main__"'?

Postby Ofnuts » Sat Jan 09, 2016 8:46 pm

... likely to avoid clashes with you own names.
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987

Re: Who else hates 'if __Name__ == "__main__"'?

Postby ichabod801 » Sat Jan 09, 2016 11:26 pm

I got no problem with it. Why make another variable when you've already got one that works? And if you have a convention for __special__ variable names, why not use it?
Due to the reasons discussed here we will be moving to python-forum.io on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
ichabod801
 
Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Re: Who else hates 'if __Name__ == "__main__"'?

Postby casevh » Sun Jan 10, 2016 5:37 am

What you are seeing is an example of Python's introspection - the ability of Python to examine its internal state.

Many Python objects (modules, classes, functions) automatically provide special attributes that provide information about the object. Here is a quick example using the math module:

Code: Select all
>>> import math
>>> math.__name__
'math'


The math module lives in a namespace called, unsurprisingly, 'math'.

Here is a simple module I called 'foo.py'.

Code: Select all
def f():
    pass

print(__name__)


If you import 'foo', its code is run inside a namespace called 'foo'.

Code: Select all
>>> import foo
foo


If you directly run 'foo.py', it is run in the top-level namespace of the Python interpreter. The top-level namespace just happens to be called '__main__'.

Code: Select all
$ py34 foo.py
__main__


The 'if __name__ == "__main__"' idiom is just comparing the name of the current namespace (the value of __name__) with the name of the top-level module ("__main__").

casevh
casevh
 
Posts: 114
Joined: Sat Feb 09, 2013 7:35 am

Re: Who else hates 'if __Name__ == "__main__"'?

Postby LjSpike » Sun Jan 10, 2016 12:43 pm

casevh wrote:What you are seeing is an example of Python's introspection - the ability of Python to examine its internal state...
...The 'if __name__ == "__main__"' idiom is just comparing the name of the current namespace (the value of __name__) with the name of the top-level module ("__main__").


That's quite a good definition there.
In order to understand recursion, one must first understand recursion.
LjSpike
 
Posts: 9
Joined: Sat Jan 09, 2016 3:34 pm
Location: A dark basement, where the only light source is that of numerous monitors

Re: Who else hates 'if __Name__ == "__main__"'?

Postby dietrich41 » Tue Feb 02, 2016 7:55 pm

A problem only if you see the "under" and not the "dunder"
User avatar
dietrich41
 
Posts: 194
Joined: Sun Jul 26, 2015 2:10 am
Location: Reno NV


Return to Bar

Who is online

Users browsing this forum: No registered users and 4 guests