Python unbound method again

This leads me to a difficult time (sorry, I'm still very new to python) Thanks for any help.

Mistake

print Student.MostFrequent() TypeError: unbound method

MostFrequent () must be called using the Student instance as the first argument (nothing worked)

This Student.MostFrequent () is called completely at the end (last line), and def is the last def in the class

EDITED - Naming Convention

My long code

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__
+3
source share
7 answers

use Student().MostFrequent()

change

Beware of using class attributes, and this is dangerous. here is an example:

>>> 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'
>>> 

as you can see i am changing marco hobbies and francesco hobbies are changing sequentially.

+7
source

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()
+7

, 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
+2

-, .

, MostFrequent . Student .

Student, .

staticmethod .

+1

(Student)

someStudent = Student(someRow)

( "" ), someStudent.

someStudent.MostFrequent()
0

Student.MostFrequent , , . , , Student(), MostFrequent() .

P.S.: - , PEP 8 most_frequent .

0

def,

def MostFrequent(self,mostFrequent):

mostFrequent, , , . :

def MostFrequent(self):
-1

Source: https://habr.com/ru/post/1725710/


All Articles