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