1

Possible Duplicate:
What does if __name__=="__main__": do?
What's the point of a main function and/or __name__ == "__main__" check in Python?

I just wanted to understand why you have you use the __name__='__main__'statement if we can run any python script even without using that statement. For example, I can run the script below without using the if __name__='__main__' statement.

def hello():
      print "hello"
      return 1234

# And here is the function being used
print hello()
wizzwizz4
  • 5,472
  • 2
  • 27
  • 55
vkaul11
  • 3,712
  • 8
  • 42
  • 67

2 Answers2

5

It's done so that code is only executed when run as a script and not when you import the module.

jamylak
  • 120,885
  • 29
  • 225
  • 225
0

Code in the global namespace runs slightly slower. It's easy to make a main() function, so why not do it? It is optional though if you don't mind the module "running" when you import it

John La Rooy
  • 281,034
  • 50
  • 354
  • 495