title: Understanding the main method of python
body: <p>I am new to Python, but I have experience in other OOP languages. My course does not explain the main method in python. </p>
  
  <p>Please tell me how main method works in python ? I am confused because I am trying to compare it to Java. </p>
  
  <pre><code>def main():
  # display some lines
  
  if __name__ == "__main__": main()
  </code></pre>
  
  <p>How is main executed and why do I need this strange <code>if</code> to execute <code>main</code>. My code is terminated without output when I remove the <code>if</code>.</p>
  
  <p>The minimal code - </p>
  
  <pre><code>class AnimalActions:
      def quack(self): return self.strings['quack']
      def bark(self): return self.strings['bark']
  
  class Duck(AnimalActions):
      strings = dict(
          quack = "Quaaaaak!",
          bark = "The duck cannot bark.",
      )
  
  
  class Dog(AnimalActions):
      strings = dict(
          quack = "The dog cannot quack.",
          bark = "Arf!",
      )
  
  def in_the_doghouse(dog):
      print(dog.bark())
  
  def in_the_forest(duck):
      print(duck.quack())
  
  def main():
      donald = Duck()
      fido = Dog()
  
      print("- In the forest:")
      for o in ( donald, fido ):
          in_the_forest(o)
  
      print("- In the doghouse:")
      for o in ( donald, fido ):
          in_the_doghouse(o)
  
  if __name__ == "__main__": main()
  </code></pre>