2016-10-23 51 views
0

這可能是愚蠢的問題,但我剛開始學OOP在Python,我努力讓這一個:類型錯誤:Class對象不是可迭代

下面的代碼工作正常:

class Name(object): 
    def __init__(self): 
     self.name=None 

C1='Stephen Curry' 
C2='Kevin Durant' 
C3='LeBron James' 
C4='Chris Paul' 


nameList=[C1,C2,C3,C4] 
nameList.sort() 
for e in nameList: 
    print (e) 

然而,當我使用nameList.sort()內的for循環,它給了我 「類型錯誤: 'NoneType' 對象不是可迭代」

class Name(object): 
    def __init__(self): 
     self.name=None 

C1='Stephen Curry' 
C2='Kevin Durant' 
C3='LeBron James' 
C4='Chris Paul' 

nameList=[C1,C2,C3,C4] 

for e in nameList.sort(): 
    print (e) 

感謝

回答

1

sort返回None,因爲它將列表排序到位。如果要在一個語句中迭代排序列表,應該使用sorted

for e in sorted(nameList): 
    pass 
相關問題