Python 显示 Jupyter 错误的代码

Python code showing Error in Jupyter

以下是 python 代码,它在命令行中显示正确输出但在 Jupyter 中显示错误。我安装了名为 Anaconda 2.4.1(64 位)的 python 发行版,其中包含 Python 2.7.11 .

以下代码采用嵌套列表并打印扁平化列表。

def printWholeList(list1):

    for iterator in list1 :
        if(isinstance(iterator,list)):
                printWholeList(iterator)
        else:
            print(iterator)

list1 = ['a' ,'b' ,['c','d',['p','h']]]
printWholeList(list1)

命令行输出:

C:\Users\KarmicSmoke>python C:\Users\KarmicSmoke\Downloads\FirstProg.py
a
b
c
d
p
h

Jupyter 中的输出:

-

--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-64-3df571b6bccc> in <module>()
      8 
      9 list1 = ['a' ,'b' ,['c','d',['p','h']]]
---> 10 printWholeList(list1)
     11 
     12 

<ipython-input-64-3df571b6bccc> in printWholeList(list1)
      2 
      3     for iterator in list1 :
----> 4         if(isinstance(iterator,list)):
      5                 printWholeList(iterator)
      6         else:

TypeError: isinstance() arg 2 must be a class, type, or tuple of classes and types

为什么会出现这个错误?

更新:
早些时候我将 'list' 声明为局部变量名,然后出现了错误。然后,我将其更改为 'list1',如上所示。这就是我在 Jupyter notebook 中所做的一切。现在我关闭它,再次打开它,瞧,它显示了正确的输出。我应该从 Jupyter 的这种行为中推断出什么? –

尝试:

def printWholeList(list1):

    for it in list1 :
        if(isinstance(it,__builtins__.list)):
                printWholeList(it)
        else:
            print(it)

list1 = ['a' ,'b' ,['c','d',['p','h']]]
printWholeList(list1)

您可能已经在笔记本中定义了一个名为 list 的变量。 不要为此使用保留关键字!