class Student:
def __init__(self, name, korean, math, english, science):
self.name = name
self.korean = korean
self.math = math
self.english = english
self.science = science
def get_sum(self):
return self.korean + self.math + self.english + self.science
def get_average(self):
return self.get_sum() / len(self.get_sum())
def __str__(self):
return "{}\t{}\t{}".format(self.name, self.get_sum(), self.get_average())
def __eq__(self, value):
return self.get_sum() == value.get_sum()
def __ne__(self, value):
return self.get_sum() != value.get_sum()
def __gt__(self, value):
return self.get_sum() > value.get_sum()
def __ge__(self, value):
return self.get_sum() >= value.get_sum()
def __lt__(self, value):
return self.get_sum() < value.get_sum()
def __le__(self, value):
return self.get_sum() <= value.get_sum()
students = [
Student("윤인성", 87,98,88,95),
Student("연하진",92,98,96,98),
Student("구지연",98,96,90,89),
Student("나선주",88,86,64,87),
Student("윤아린",98,99,78,80),
]
student_a = Student("윤인성", 87,98,88,95)
student_b = Student("연하진",92,98,96,98)
print("student_a == student_b", student_a == student_b)
print("student_a != student_b", student_a != student_b)
print("student_a > student_b", student_a > student_b)
print("student_a >= student_b", student_a >= student_b)
print("student_a <= student_b", student_a <= student_b)
print("student_a < student_b", student_a < student_b)
The code defines a `Student` class with methods to calculate total scores, average scores, and implements special methods to compare two `Student` instances based on their total scores. However, there's a mistake in the implementation of the `get_average` method that needs to be corrected for the code to work as intended.
### Correction Needed for `get_average`
- **Original Line**: `return self.get_sum() / len(self.get_sum())`
This line attempts to divide the total score by the length of the total score, which is incorrect because `self.get_sum()` returns an integer, and `len()` cannot be applied to integers.
- **Corrected Line**: The intention is to divide the total score by the number of subjects, which is 4. Therefore, the correct implementation should be:
def get_average(self):
return self.get_sum() / 4
### Special Methods for Comparison
- The class correctly implements the special methods `__eq__`, `__ne__`, `__gt__`, `__ge__`, `__lt__`, and `__le__` to allow for comparison of `Student` instances based on their total scores. These methods make it possible to use comparison operators (==, !=, >, >=, <, <=) directly on `Student` instances.
### Demonstrating Comparisons
- The script creates two `Student` instances, `student_a` and `student_b`, and demonstrates comparing them using the implemented special methods. This shows whether one student has a higher total score than the other, if their total scores are equal, etc.
### Corrected `Student` Class Implementation
Here is the corrected implementation for the `get_average` method:
class Student:
def __init__(self, name, korean, math, english, science):
self.name = name
self.korean = korean
self.math = math
self.english = english
self.science = science
def get_sum(self):
return self.korean + self.math + self.english + self.science
def get_average(self):
return self.get_sum() / 4
def __str__(self):
return "{}\t{}\t{}".format(self.name, self.get_sum(), self.get_average())
def __eq__(self, value):
return self.get_sum() == value.get_sum()
def __ne__(self, value):
return self.get_sum() != value.get_sum()
def __gt__(self, value):
return self.get_sum() > value.get_sum()
def __ge__(self, value):
return self.get_sum() >= value.get_sum()
def __lt__(self, value):
return self.get_sum() < value.get_sum()
def __le__(self, value):
return self.get_sum() <= value.get_sum()
```
### Usage and Output
Using the corrected `Student` class, the comparison print statements at the end will work as expected, displaying whether `student_a` and `student_b` are equal, not equal, or one is greater than or less than the other, based on their total scores. This is a practical application of class customization and operator overloading in Python, making object comparison intuitive and readable.
'개념 > 혼자 공부하는 파이썬' 카테고리의 다른 글
python) Put only things that can be converted to numbers into a list (2) | 2024.10.30 |
---|---|
python) Overriding (1) | 2024.10.29 |
python) Declare a function inside a class (0) | 2024.10.27 |
python) class parameter (0) | 2024.10.26 |
python) class function (1) | 2024.10.25 |