title: Why am I getting output twice?
body: <p>Asking this question here, as the previous one was closed: <a href="https://stackoverflow.com/questions/41315555/traversing-a-linked-list-getting-the-output-twice">https://stackoverflow.com/questions/41315555/traversing-a-linked-list-getting-the-output-twice</a></p>
  
  <p>Update: I am using Python 3.6, Eclipse Neon.2 Release (4.6.2) (32 bit), and PyDev 5.5</p>
  
  <p>I have created two classes, Node.py and UnorderedList.py.</p>
  
  <p>Node.py is as follows:</p>
  
  <pre><code>class Node:
      #constructor
      def __init__(self,initdata):
          self.data = initdata
          self.next = None
  
      def hasNext(self):
          return self.next != None
  </code></pre>
  
  <p>UnorderedList.py :</p>
  
  <pre><code>class UnorderedList:   
      def __init__(self):
          self.head = None
          self.last = None
  
      def append(self,data):  
          temp = Node(data)
          if(self.head == None):
              self.head = temp
              self.last = self.head
          else:
              self.last.next = temp
              self.last = self.last.next
  
      def traverse(self):
          current= self.head
          while current != None:
              print(current.data)
              current = current.next
  </code></pre>
  
  <p>I am testing it, using the following code:</p>
  
  <pre><code>ul = UnorderedList()
  ul.append(1)
  ul.append(2)
  ul.traverse() 
  </code></pre>
  
  <p>When I insert both classes in a single Python script, and run the code, the output, as expected, is:</p>
  
  <pre><code>1
  2
  </code></pre>
  
  <p>However, when</p>
  
  <ol>
  <li>I put the two classes in different modules, in the following package structure,</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/qWJSo.png" rel="nofollow noreferrer">Package structure</a></p>
  
  <ol start="2">
  <li><p>put the import line on top of UnorderedList.py:</p>
  
  <pre><code>import py_linked_lists.Node as Node
  </code></pre></li>
  <li><p>make changes to append() as follows:</p>
  
  <pre><code>def append(self,data):
      temp = Node.Node(data)
      #rest all code remains same
  </code></pre></li>
  <li><p>Run the code, I get output twice:</p>
  
  <pre><code>1
  2
  1
  2
  </code></pre></li>
  </ol>
  
  <p>Update: 
  I tried out the suggestions in comments in <a href="https://stackoverflow.com/questions/41315555/traversing-a-linked-list-getting-the-output-twice">https://stackoverflow.com/questions/41315555/traversing-a-linked-list-getting-the-output-twice</a>, and was successful in preventing the output from coming twice, by using the <code>if __name__ == "__main__"</code><br>
  guard block, as follows, in UnorderedList.py:</p>
  
  <pre><code>if __name__ == "__main__":
  
      ul = UnorderedList()
      ul.append(1)
      ul.append(2)
      ul.traverse()
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>1
  2
  </code></pre>
  
  <p>Thanks to @Blckknght for that.</p>
  
  <p>As i saw in the answers here, <a href="https://stackoverflow.com/questions/419163/what-does-if-name-main-do">What does if __name__ == &quot;__main__&quot;: do?</a>, in case I do not use <code>if __name__ == "__main__"</code>, the <code>Node</code> class which I import from <code>py_linked_lists.Node</code> will also get executed. </p>
  
  <p>What I am not able to understand is how execution of <code>Node.py</code> impacts the output, when there is no such code there, just a class structure present.</p>