Re: [Tutor] question about "__main__"

2015-11-16 Thread Alan Gauld

On 16/11/15 15:30, CUONG LY wrote:

Hello,

I’m learning Python.


Hello, welcome.


I want to know what does the following line represent
if __name__ == ‘__main__’:


The short answer is that it tests whether the file is being
imported as a module or executed as a program.
If its being imported as a module its __name__ attribute will
be set to the name of the module.

If its being executed as the main (top level) program file
its __name__ will be set to "__main__".

So when you see:

if __name__ == ‘__main__’:
# some code here

the 'some code here' block will only be executed if the file
is being executed as a program. It will not be executed
if the file is being imported by another file.

That will only make sense if you have come across modules yet.
If not don't worry about it, you will get there eventually.
Then it will all become clear.

> and why I see some python codes have two of them ?

I don't know, I've never seen two.
But it is perfectly acceptable to do it, just a
little pointless - unless perhaps they are inside
a function or something.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] question about "__main__"

2015-11-16 Thread CUONG LY
Hello,

I’m learning Python. 

I want to know what does the following line represent and why I see some python 
codes have two of them ?

if __name__ == ‘__main__’:




Thanks in advance

Cuong
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor