2009-12-15 63 views
3

這讓我陷入困難時期(對不起,我還是很新的python) 謝謝你的任何幫助。python unbound方法再次

錯誤

print Student.MostFrequent() TypeError: unbound method 

MostFrequent()必須與 學生例如被稱爲第一個參數 (什麼都沒有代替)

這Student.MostFrequent()被調用所有方式在最後(最後一行)和高清是最後清晰的類

編輯 - 命名修道院離子

我的長碼

import csv 
class Student: 
    sports = [] 
    ftopics = [] 
    stopics = [] 
    choice_list = [] 
    choice_dict = {} 
    def __init__(self, row): 
     self.lname, self.fname, self.ID, self.gender, self.sport, self.movie, self.movieyr, self.country, self.ftopic, self.stopic = row 
     self.sports.append(self.sport) 
     self.ftopics.append(self.ftopic) 
     self.stopics.append(self.stopic) 
    def print_information(self): 
     return (self.lname, self.fname, self.ID, self.gender) 
    def print_first(self): 
     return (self.lname, self.fname, self.sport) 
    def print_second(self): 
     return (self.lname, self.fname, self.movie, self.movieyr) 
    def print_third(self): 
     return (self.lname, self.fname, self.country) 
    def print_fourth(self): 
     return (self.lname, self.fname, self.ftopic, self.stopic) 
    def most_frequent(self): 
     for choice in self.choice_list: 
       self.choice_dict[choice] = self.choice_dict.get(choice, 0) + 1 
     self.mostFrequent = sorted([(v, k) for k, v in self.choice_dict.items()], reverse=True) 
     print self.mostFrequent 

reader = csv.reader(open('new_mondy_csc_data_revise.csv'), delimiter=',', quotechar='"') 
header = tuple(reader.next()) 
print "%-17s|%-10s|%-6s|%s" %header[:4] 
print "-" * 45 
students = list(map(Student, reader)) # read all remaining lines 
for student in students: 
    print "%-17s|%-10s|%-6s|%3s" % student.print_information() 

print "%-17s|%-10s|%s" %(header[0],header[1],header[4]) 
print "-" * 45 
for student in students: 
    print "%-17s|%-10s|%s" %student.print_first() 

print "%-17s|%-10s|%-16s|%s" %(header[0],header[1],header[5],header[6]) 
print "-" * 45 
for student in students: 
    print "%-17s|%-10s|%-16s|%s" % student.print_second() 

print "%-17s|%-10s|%s" %(header[0],header[1],header[7]) 
print "-" * 45 
for student in students: 
    print "%-17s|%-10s|%s" %student.print_third() 

print "%-17s|%-10s|%-15s|%s" %(header[0],header[1],header[8],header[9]) 
print "-" * 45 
for student in students: 
    print "%-17s|%-10s|%-16s|%s" % student.print_fourth() 

k = len(students)  
# Printing all sports that are specified by students 
for s in set(Student.sports): # class attribute 
    print s, Student.sports.count(s), round(((float(Student.sports.count(s))/k) *100),1) 

# Printing sports that are not picked 
allsports = ['Basketball','Football','Other','Baseball','Handball','Soccer','Volleyball','I do not like sport'] 
allsports.sort() 
for s in set(allsports) - set(Student.sports): 
    print s, 0, '0%' 
Student.choice_list = Student.sports 
X = Student() 
X.most_frequent() 

#class Search(Student): 
# def __init__(self): 
#  Student.__init__ 

回答

7

使用Student().MostFrequent()

編輯

提防您使用類屬性,這是很危險的。這裏舉一個例子:

>>> class Person: 
... name = None 
... hobbies = [] 
... def __init__(self, name): 
... self.name = name 
... 
>>> a = Person('marco') 
>>> b = Person('francesco') 
>>> a.hobbies.append('football') 
>>> b.hobbies 
['football'] 
>>> a.name 
'marco' 
>>> b.name 
'francesco' 
>>> a.name = 'mario' 
>>> b.name 
'francesco' 
>>> a.name 
'mario' 
>>> 

正如你所看到的我修改marco的愛好和francesco的愛好被修改後必然。

+0

確實有一個構造這項工作,如:高清__init __(自我,行)? – miku 2009-12-15 11:58:35

+0

以及steve如何從方法定義中刪除mostFrequent參數,但是您得到的錯誤與您沒有實例類 – 2009-12-15 12:01:06

+0

@The MYYN:no。的事實有關。也許我匆忙回答這個問題,但他得到了這個錯誤,因爲他調用了一個方法而不是一個對象。通過類調用方法有'classmethod'和'staticmethod'裝飾器。請參閱有關其使用的文檔。 – 2009-12-15 12:07:43

-1

你們班閃避,方法定義

def MostFrequent(self,mostFrequent): 

有額外的變量mostFrequent,你可能不希望在那裏。嘗試更改爲:

def MostFrequent(self): 
+0

我猜在這種情況下,這會抱怨錯誤的參數數量,或不? – miku 2009-12-15 12:07:05

+0

總是很高興看到一個適合休斯頓的CS畢業生 – 2009-12-15 12:13:24

0

你很少調用一個類定義的方法(Student

幾乎總是創建類的實例

someStudent = Student(someRow) 

然後你調用的方法實例(「對象」),someStudent

someStudent.MostFrequent() 
7

第一命名慣例閱讀PEP-8

方法名稱和實例變量

Use the function naming rules: lowercase with words separated by 
    underscores as necessary to improve readability. 

第二你是在類Student調用mostFrequest,而不是插件它的意義。改爲在實例上使用該方法:

student = Student(row) 
student.MostFrequent() 
0

Student.MostFrequent表示您試圖使用靜態方法,而不是實例方法。所以你必須先調用Student()來創建實例,然後調用MostFrequent()。

P.S .:如果這不是某個神祕項目的一部分,我建議您遵循PEP 8並使用most_frequent作爲方法名稱。

1

首先,我建議只使用函數名稱小寫。

將使用MostFrequent的結果作爲靜態方法得到的結果。爲此,您需要明確地傳遞一個Student的實例作爲第一個參數。

如果直接調用Student的實例,該實例將作爲第一個參數隱式傳遞。

考慮使用staticmethod裝飾器來靜態使用函數。

2

你可能想要的是定義most_frequentclassmethod

@classmethod 
def most_frequent(cls): 
    for choice in cls.choice_list: 
     cls.choice_dict[choice] = cls.choice_dict.get(choice, 0) + 1 
    cls.mostFrequent = sorted([(v, k) for k, v in cls.choice_dict.items()], reverse=True) 
    return cls.mostFrequent