再次使用python unbound方法

时间:2009-12-15 11:51:29

标签: python class

这让我陷入困境(对不起,我对python还是很新) 感谢您提供任何帮助。

错误

print Student.MostFrequent() TypeError: unbound method
     

必须使用调用MostFrequent()   学生实例作为第一个参数   (没有得到任何东西)

这个Student.MostFrequent()最后一直被调用(最后一行),def是该类中的最后一个def

已编辑 - 命名惯例

我的长码

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 个答案:

答案 0 :(得分: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的爱好也随之得到了修改。

答案 1 :(得分:7)

首先阅读有关命名约定的PEP-8

  

方法名称和实例变量

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

第二次您正在课程mostFrequest上调用Student,而不是它的实例。请在实例上使用该方法:

student = Student(row)
student.MostFrequent()

答案 2 :(得分:2)

您可能希望将most_frequent定义为classmethod

@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

答案 3 :(得分:1)

首先,我建议仅将函数名称设为小写。

使用MostFrequent作为静态方法会导致错误。为此,您需要显式传递Student的实例作为第一个参数。

如果直接在Student的实例上调用,则实例将隐式作为第一个参数传递。

考虑使用staticmethod装饰器来静态使用函数。

答案 4 :(得分:0)

您很少在类定义(Student

上调用方法

几乎总是,你创建了一个类的实例

someStudent = Student(someRow)

然后在实例(“对象”)上调用方法,someStudent

someStudent.MostFrequent()

答案 5 :(得分:0)

Student.MostFrequent表示您尝试使用静态方法,而不是实例方法。因此,您必须首先通过调用Student()创建实例,然后在其上调用MostFrequent()。

P.S。:如果这不是某个神秘项目的一部分,我建议您遵循PEP 8并使用most_frequent作为方法名称。

答案 6 :(得分:-1)

在您的类def中,方法定义

def MostFrequent(self,mostFrequent):

有你可能不想要的额外变量 mostFrequent 。尝试更改为:

def MostFrequent(self):