Python 3.x 在Python 3.43中定义类方法
我试图定义一个名为Student的类,该类包含名称、ID号、测试分数和平均测试分数。当我运行这个程序时,除了在新的分数被添加到课堂上的原始分数列表后打印测试分数平均值之外,其他一切都正常工作。它保持了“未定义”的原始平均值,我不明白为什么它没有更新Python 3.x 在Python 3.43中定义类方法,python-3.x,Python 3.x,我试图定义一个名为Student的类,该类包含名称、ID号、测试分数和平均测试分数。当我运行这个程序时,除了在新的分数被添加到课堂上的原始分数列表后打印测试分数平均值之外,其他一切都正常工作。它保持了“未定义”的原始平均值,我不明白为什么它没有更新 class Student: """The Student class stores the first and last name of a student, as well as all of their exam scores and
class Student:
"""The Student class stores the first and last name of a student,
as well as all of their exam scores and overall average."""
def __init__(self, Id, first='', last=''):
"""Create a student object (e.g., Student(123654, Jane, Doe))"""
self.Id = Id
self.first_name = first
self.last_name = last
self.scores = []
self.average = 'Undefined'
def getId(self):
"""Retrieves student's Id number"""
return self.Id
def getfirst(self):
"""Retrieves student's first name"""
return self.first_name
def getlast(self):
"""Retrieves student's last name"""
return self.last_name
def getscore(self):
"""Retrieves list of student's test scores"""
return self.scores
def getaverage(self):
"""Retrieves student's average test score"""
return self.average
def add_score(self, score):
"""Updates student's list of test scores"""
self.scores.append(score)
return self.scores
def calculate_average(self):
"""Updates student's average test score using updated list of scores"""
self.average = sum(self.scores) / len(self.scores)
return self.average
def __str__(self):
"""Organizes student's information into a printable statement"""
return "ID: " + self.Id + " Name: " + self.first_name + " " + \
self.last_name + " Average Score: " + self.average
def main():
main() 试一试
其余的方法是不必要的,因为(我假设)您只想打印值,而不想将其存储在变量中 下面是一个更具python风格的类定义:
from math import nan
from statistics import mean, StatisticsError
class Student(object):
"""The Student class stores the first and last name of a student,
as well as all of their exam scores and overall average."""
def __init__(self, Id, first='', last=''):
"""Create a student object (e.g., Student(123654, Jane, Doe))"""
self.Id = Id
self.first = first
self.last = last
self.scores = []
def __repr__(self):
"""Organizes student's information into a printable statement"""
return "{0.__class__.__name__}({0.Id}, '{0.first}', '{0.last}', {0.average})".format(self)
def __str__(self):
"""Organizes student's information into a printable statement"""
return "{0.__class__.__name__}({0.Id}, '{0.first}', '{0.last}', {0.average})".format(self)
@property
def average(self):
try:
return mean(self.scores)
except StatisticsError:
return nan
def add_score(self, score):
"""Updates student's list of test scores"""
self.scores.append(score)
通过使用@property
装饰器,self.average
是一个将自行计算的属性。这有助于避免“self.average是否已更新?”的问题,因为所有计算都是由属性本身完成的。此外,如果没有分数,也可以使用nan
(非数字)
for student in student_list:
student.calculate_average()
print(student)
from math import nan
from statistics import mean, StatisticsError
class Student(object):
"""The Student class stores the first and last name of a student,
as well as all of their exam scores and overall average."""
def __init__(self, Id, first='', last=''):
"""Create a student object (e.g., Student(123654, Jane, Doe))"""
self.Id = Id
self.first = first
self.last = last
self.scores = []
def __repr__(self):
"""Organizes student's information into a printable statement"""
return "{0.__class__.__name__}({0.Id}, '{0.first}', '{0.last}', {0.average})".format(self)
def __str__(self):
"""Organizes student's information into a printable statement"""
return "{0.__class__.__name__}({0.Id}, '{0.first}', '{0.last}', {0.average})".format(self)
@property
def average(self):
try:
return mean(self.scores)
except StatisticsError:
return nan
def add_score(self, score):
"""Updates student's list of test scores"""
self.scores.append(score)