+ 1
Why does the magic method __gt__ not only compare 2 objects salaries but compare any 2 numeric arguments of 2 different objects?
The __gt__ magic method Compares any numeric variable from the self instance with any numeric variable other instance. I think it should just compare their salaries only. class Employee: def __init__(self, name, age, gender, level, salary): self.name = name self.age = age self.gender = gender self.level = level self.salary = salary def salary_up(self): return self.salary * 1.05 def year5(self): return self.age - 5 def __gt__(self, other): return self.level > other.level tony = Employee("Tony", 24, "Male", 8, 7500) kenny = Employee("Kenny", 19, "Male", 6, 5000) print(tony.salary > kenny.age)
3 Antworten
+ 2
Ipang I was expecting the magic method __gt__ to compare only 2 salaries but it end up comparing any 2 numeric arguments of 2 different objects.
+ 2
Rebecca
Is this one from the tutorial? I don't remember this one.
If you want __gt__ to compare by salary then simply replace the attribute used in the returned expression - from this
return self.level > other.level
into this ...
return self.salary > other.salary
But ... if this is from a lesson, or quiz; I don't think it's right to change how it works, depending on task requirement of course ...
+ 1
Do you have any question related to that code? cause I don't see a problem running it.