简体   繁体   中英

Python code showing Error in Jupyter

The following is the python code that was showing correct output in command line but showing error in Jupyter. I installed python distribution called Anaconda 2.4.1 (64-bit) containing Python 2.7.11 .

The following code takes a nested list and prints the flattened list.

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)

Output in command line :

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

Output in 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

Why do this error occurs ?

Update :
Earlier I declared 'list' as local variable name and that error came . Then , I changed it to 'list1' as you can see above. This all I did in Jupyter notebook. Now I closed it , opened it again and VOILA it shows correct output . What should i infer from this behaviour of Jupyter ?

Try :

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)

You have probably defined a var named list in your notebook. Don't use reserved keywords for that !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM