为什么我在以下代码中使用的相同 Magic 方法生成的输出与预期不同
Why is the same Magic method I used in the following code generating different output than what is expected
class Time:
def __init__(self, hour=0, minute=0, second=0):
self.hour = hour
self.minute = minute
self.second = second
def __str__(self):
return "{}:{:02d}:{:02d}".format(self.hour, self.minute, self.second)
def __add__(self, other_time):
new_time = Time()
if (self.second + other_time.second) >= 60:
self.minute += 1
new_time.second = (self.second + other_time.second) - 60
else:
new_time.second = self.second + other_time.second
if (self.minute + other_time.minute) >= 60:
self.hour += 1
new_time.minute = (self.minute + other_time.minute) - 60
else:
new_time.minute = self.minute + other_time.minute
if (self.hour + other_time.hour) > 24:
new_time.hour = (self.hour + other_time.hour) - 24
else:
new_time.hour = self.hour + other_time.hour
return new_time
def __sub__(self, other_time):
new_time = Time()
if (self.second + other_time.second) >= 60:
self.minute += 1
new_time.second = (self.second + other_time.second) - 60
else:
new_time.second = self.second + other_time.second
if (self.minute + other_time.minute) >= 60:
self.hour += 1
new_time.minute = (self.minute + other_time.minute) - 60
else:
new_time.minute = self.minute + other_time.minute
if (self.hour + other_time.hour) > 24:
new_time.hour = (self.hour + other_time.hour) - 24
else:
new_time.hour = self.hour + other_time.hour
return new_time
def main():
time1 = Time(1, 20, 10)
print(time1)
time2 = Time(24, 41, 30)
print(time1 + time2)
print(time1 - time2)
main()
在class的时候,我定义了方法add和sub,功能相同(基本上就是不同调用的相同方法)。
当我 运行 print(time1 + time2), print(time1 -time2) 我希望得到相同的输出。但相反,我最终得到以下输出。
Output:
1:20:10
2:01:40
3:01:40
expected Output:
1:20:10
2:01:40
2:01:40
如果有人能指出一种方法来解释上面代码生成的输出,或者解释当我 运行 上面的代码导致产生的输出。
行 self.minute += 1
和 self.hour += 1
表示您的 __add__
和 __sub__
都将左侧参数修改为 +
/-
.因此,在评估 time1 + time2
之后, time1
的值将不同,因此您在评估 time1 - time2
.
时会得到不同的答案
我尝试了您的代码并发现了以下内容:在指令之后
Time(time1+time2)
是 运行,time1 的值现在是 2:20:10,所以当第二种方法完成时,小时数将是 (24+2)-24 而不是 (24+1)- 24
class Time:
def __init__(self, hour=0, minute=0, second=0):
self.hour = hour
self.minute = minute
self.second = second
def __str__(self):
return "{}:{:02d}:{:02d}".format(self.hour, self.minute, self.second)
def __add__(self, other_time):
new_time = Time()
if (self.second + other_time.second) >= 60:
self.minute += 1
new_time.second = (self.second + other_time.second) - 60
else:
new_time.second = self.second + other_time.second
if (self.minute + other_time.minute) >= 60:
self.hour += 1
new_time.minute = (self.minute + other_time.minute) - 60
else:
new_time.minute = self.minute + other_time.minute
if (self.hour + other_time.hour) > 24:
new_time.hour = (self.hour + other_time.hour) - 24
else:
new_time.hour = self.hour + other_time.hour
return new_time
def __sub__(self, other_time):
new_time = Time()
if (self.second + other_time.second) >= 60:
self.minute += 1
new_time.second = (self.second + other_time.second) - 60
else:
new_time.second = self.second + other_time.second
if (self.minute + other_time.minute) >= 60:
self.hour += 1
new_time.minute = (self.minute + other_time.minute) - 60
else:
new_time.minute = self.minute + other_time.minute
if (self.hour + other_time.hour) > 24:
new_time.hour = (self.hour + other_time.hour) - 24
else:
new_time.hour = self.hour + other_time.hour
return new_time
def main():
time1 = Time(1, 20, 10)
print(time1)
time2 = Time(24, 41, 30)
print(time1 + time2)
print(time1 - time2)
main()
在class的时候,我定义了方法add和sub,功能相同(基本上就是不同调用的相同方法)。 当我 运行 print(time1 + time2), print(time1 -time2) 我希望得到相同的输出。但相反,我最终得到以下输出。
Output:
1:20:10
2:01:40
3:01:40
expected Output:
1:20:10
2:01:40
2:01:40
如果有人能指出一种方法来解释上面代码生成的输出,或者解释当我 运行 上面的代码导致产生的输出。
行 self.minute += 1
和 self.hour += 1
表示您的 __add__
和 __sub__
都将左侧参数修改为 +
/-
.因此,在评估 time1 + time2
之后, time1
的值将不同,因此您在评估 time1 - time2
.
我尝试了您的代码并发现了以下内容:在指令之后
Time(time1+time2)
是 运行,time1 的值现在是 2:20:10,所以当第二种方法完成时,小时数将是 (24+2)-24 而不是 (24+1)- 24