Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPiyush Chawla
Courses Plus Student 30 PointsI havn't understood some part of the video
what is the meaning of - if name='main':
1 Answer
John Lindsey
15,642 Pointsif __name__ == '__main__':
main()
In short, this will run the main() function (In my example. The main() can be replaced with whatever you called your main function) when the script is being run. Whenever a script is run, Python automatically sets the name equal to 'main'. Now this comes in handy when you import other files. The imported file's name will not be set equal to 'main', so it will not pass the test and it's main function will not be executed. This helps when you just want to use a part of another script that you have written (say just a class), but you don't want to run the main function of that file the second it was imported. And just because I feel like I'm still not explaining it well enough, here is a link to another explanation that may be more helpful: http://stackoverflow.com/questions/419163/what-does-if-name-main-do . Basically, it just prevents imported scripts from being executed right when they are imported.
Michal Janek
Front End Web Development Techdegree Graduate 30,654 PointsWhy is this workshop the first time we are seeing this? :O
Piyush Chawla
Courses Plus Student 30 PointsPiyush Chawla
Courses Plus Student 30 Pointssorry it was- if name=='main':