2013-12-12 96 views
1

我是新來的蟒蛇,我正在閱讀python文檔,並在給定的例子http://docs.python.org/2/howto/sorting.html#key-functions我卡住,我沒有得到它如何正確工作。Python類不能使用排序功能

我使用eclipse Pydev的運行此代碼..

class Student: 

    student_object = [ 
        ('john', 'a', 15), 
        ('as', 'C', 12), 
        ('dave', 'B', 10) 
        ] 

    def __init__(self, name, grade, age): 
     self.name = name 
     self.grade = grade 
     self.age = age 


    def __repr__(self): 
     return repr((self.name, self.grade, self.age)) 



print sorted(Student.student_object, key=lambda Student: Student.age) 

錯誤

Traceback (most recent call last): 
    File "C:\Users\user1\workspace\demPython\src\ru.py", line 26, in <module> 
    print sorted(Student.student_object, key=lambda Student: Student.age) 
    File "C:\Users\user1\workspace\demPython\src\ru.py", line 26, in <lambda> 
    print sorted(Student.student_object, key=lambda Student: Student.age) 
AttributeError: 'tuple' object has no attribute 'age' 

如何類和方法在Python的工作?爲什麼這段代碼不工作?

編輯

但我changein最後一行,並與給定的行

print sorted(Student.student_object, key=lambda student: student[0]) 

它是工作,給我輸出[('as', 'C', 12), ('dave', 'B', 10), ('john', 'a', 15)]

+3

你混淆了文檔中的例子;重新仔細閱讀它們。 'student_tuples'與基於類的示例完全分開。 – jonrsharpe

回答

4

它不起作用,因爲student_object中的項目不是您的Student類的實例,它們只是普通的元組。這就是爲什麼錯誤是'tuple' object has no attribute 'age'

您需要創建Student對象第一:

class Student(object): 

    def __init__(self, name, grade, age): 
     self.name = name 
     self.grade = grade 
     self.age = age 

    def __repr__(self): 
     return '{0.name} {0.grade} {0.age}'.format(self) 

students = [Student('john','a',15), Student('as','C',12), Student('dave','B',10)] 
print sorted(students, key=lambda x: x.age) 
2

沒有現場叫age更換,在你的student_object。所以,你需要這個

print sorted(Student.student_object, key=lambda Student: Student[2]) 

或者,你可以使用itemgetter這樣

from operator import itemgetter 
getage = itemgetter(2) 
print sorted(Student.student_object, key = getage) 

注:不要將其命名以首字母大寫的變量。

+0

但是有場年齡? 'self.age = age' – KumarDharm

+0

@KumarDharm但是你想排序,只有'student_object',對吧? – thefourtheye

+1

* note備註+1 * - 重要建議,因爲這是PEP中定義的標準做法 –

1

Student.student_object只是

student_object = [ 
       ('john', 'a', 15), 
       ('as', 'C', 12), 
       ('dave', 'B', 10) 
       ] 

和無關與學生類,除了它是「內部」它。類似於

class Student: 

    def __init__(self, name, grade, age): 
     self.name = name 
     self.grade = grade 
     self.age = age 


    def __repr__(self): 
     return repr((self.name, self.grade, self.age)) 



student_object = [ Student('john', 'a', 15), 
        Student('as', 'C', 12), 
        Student('dave', 'B', 10) ] 


print sorted(student_object, key=lambda x: x.age) 

相反,因爲數組的每個元素都是一個Student實例,所以定義了attr的年齡。

1

類定義了對象的結構和行爲,也就是實例。當你定義一個類,Student,就像你在這裏做的那樣,Python創建一個函數Student(),稱爲初始化器,你可以使用它來創建該類的實例。由於您爲Student提供了一個__init__方法,因此Python將安排將其作爲初始化程序的實現進行調用。

因此,要創建一個Student對象,您需要用您的__init__方法指定的參數調用Student的初始化程序。例如:Student('john', 'A', 15)

您的鍵功能(即您使用lambda表達式創建的匿名功能)預計會收到age屬性。據推測,這應該是一個對象,它是學生類的一個實例。但是:

  1. 將參數命名爲Student簡直是錯誤的。學生在這裏沒有提到課程;它就像任何其他函數參數一樣,只是給你在這個函數中接收的參數一個名字。請注意,與某些語言不同,當您在Python中聲明參數時,不必提及類型名稱和變量名稱;你只需聲明變量名並開始使用它。還要注意,以大寫字母命名變量是非常糟糕的Python風格,因爲這個原因:它混淆了人們關於什麼是變量,什麼是類,等等。
  2. 實際上被傳遞給你的關鍵函數的對象是不是學生。由於您在元組列表上調用sorted(),函數接收的元素是元組,您可以將它看作Python提供的內置tuple類型的實例。

因此,首先要關注獲取學生列表,並準確理解創建對象的工作原理。一旦你這樣做,就像你所做的那樣,打電話給sorted就行了。然後,修復你的關鍵功能,爲了理智的緣故使用一個小寫的student參數,你很好走!