【问题标题】:Why am I getting output twice?为什么我得到两次输出?
【发布时间】:2017-06-22 08:21:59
【问题描述】:

在这里问这个问题,因为上一个已关闭:https://stackoverflow.com/questions/41315555/traversing-a-linked-list-getting-the-output-twice

更新:我正在使用 Python 3.6、Eclipse Neon.2 Release (4.6.2)(32 位)和 PyDev 5.5

我创建了两个类,Node.py 和 UnorderedList.py。

Node.py如下:

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

我正在测试它,使用以下代码:

ul = UnorderedList()
ul.append(1)
ul.append(2)
ul.traverse() 

当我在一个 Python 脚本中插入这两个类并运行代码时,如预期的那样,输出是:

1
2

然而,当

  1. 我把这两个类放在不同的模块中,包结构如下,

Package structure

  1. 将导入行放在 UnorderedList.py 的顶部:

    import py_linked_lists.Node as Node
    
  2. 对 append() 进行如下修改:

    def append(self,data):
        temp = Node.Node(data)
        #rest all code remains same
    
  3. 运行代码,我得到了两次输出:

    1
    2
    1
    2
    

更新: 我在 https://stackoverflow.com/questions/41315555/traversing-a-linked-list-getting-the-output-twice 中尝试了 cmets 中的建议,并通过使用 if __name__ == "__main__"
成功地防止了输出两次 保护块,如下,在UnorderedList.py中:

if __name__ == "__main__":

    ul = UnorderedList()
    ul.append(1)
    ul.append(2)
    ul.traverse()

输出:

1
2

感谢@Blckknght。

正如我在这里的答案中看到的,What does if __name__ == "__main__": do?,如果我不使用if __name__ == "__main__",我从py_linked_lists.Node 导入的Node 类也将被执行。

我无法理解的是Node.py 的执行如何影响输出,当那里没有这样的代码时,只存在一个类结构。

【问题讨论】:

  • 无论是在此处还是在原始问题中,您都没有说明__init__.py 中的内容。是空的吗?
  • __init__.py 包含:import py_linked_lists.UnorderedList as UnorderedList

标签: python eclipse python-3.x pydev


【解决方案1】:

我从@nitind 的评论中得到了答案。 我在__init__.pypy_linked_lists 中有以下内容:

import py_linked_lists.UnorderedList as UnorderedList

我从https://stackoverflow.com/a/448311/4515198 了解到,每次我们在包中运行一个模块时,都会加载包的__init__.py

在这种情况下,py_linked_lists__init__.py 将在模块UnorderedList.py 执行之前加载。因此,UnorderedList.py 模块作为 import__init__.py 加载,然后作为 ma​​in 模块执行。因此,我得到了两次输出。

为了确认,我在UnorderedList.py中添加了以下内容:

if __name__ == "__main__":
    print("Name is",__name__) 
    ul = UnorderedList()
    ul.append(1)
    ul.append(2)
    ul.traverse()
else:
    print("Name is",__name__)
    print("UnorderedList also being imported")
    print("---------------------------------")

输出:

Name is py_linked_lists.UnorderedList
UnorderedList also being imported
---------------------------------
Name is __main__
1
2

这说明,UnorderedList.py首先 加载的 import from __init__.py,因此__name__ 等于py_linked_lists.UnorderedList,因此,else 部分被执行。

然后,模块 UnorderedList.py 被加载为 ma​​in 模块,因此 __name__ 等于 __main__ 并且 if 部分被执行,证明了上面的输出。

【讨论】:

    猜你喜欢
    • 2021-01-09
    • 2021-01-08
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 2020-07-20
    相关资源
    最近更新 更多