When do I use if __name__ == '__main__'?

0 Eden Hazard · June 30, 2015
When should I use this boilerplate code?

if __name__ == '__main__'

Post a Reply


Oldest  Newest  Rating
0 Halcyon Abraham Ramirez · June 30, 2015
usually thats used if your importing stuff

because if you didn't put that code and you imported your module.

I guess the module you imported would automatically run

at least thats how I understood it
+2 alex constantin · June 30, 2015
Halcyon Abraham Ramirez does have a point. It can be used like a main() method (if you come from Java or C++), which means it is the first function that is called.
0 Otto Von Chesterfield · August 9, 2015
Long story short, you put some test code inside that if block if you're writing a module or package.
  • 1



This section is all about snakes! Just kidding.

Bucky Roberts Administrator