Python 3.52 中的字符串或对象比较
String or object compairson in Python 3.52
我正在做 exorcism.io 时钟练习,我不明白为什么这个测试失败了。结果看起来完全相同,甚至具有相同的类型。
这是我的代码:
class Clock:
def __init__(self, h, m):
self.h = h
self.m = m
self.adl = 0
def make_time(self):
s = self.h * 3600
s += self.m * 60
if self.adl: s += self.adl
while s > 86400:
s -= 86400
if s == 0:
return '00:00'
h = s // 3600
if h:
s -= h * 3600
m = s // 60
return '{:02d}:{:02d}'.format(h, m)
def add(self, more):
self.adl = more * 60
return self.make_time()
def __str__(self):
return str(self.make_time()) # i don't think I need to do this
if __name__ == '__main__':
cl1 = Clock(34, 37) #10:37
cl2 = Clock(10, 37) #10:37
print(type(cl2))
print(cl2, cl1)
print(cl2 == cl1) #false
没有 __eq__
method 的自定义 class 默认测试 身份 。也就是说,仅当引用完全相同的对象时,对此类 class 实例的两个引用才相等。
您需要定义一个自定义 __eq__
方法,当两个实例包含相同时间时 returns True
:
def __eq__(self, other):
if not isinstance(other, Clock):
return NotImplemented
return (self.h, self.m, self.adl) == (other.h, other.m, other.adl)
通过为非 Clock
实例(或子 class)返回 NotImplemented
单例,您让 Python 知道 other
对象也可以被要求测试是否相等。
但是,您的代码接受大于正常小时和分钟范围的值;与其存储小时和分钟,不如存储秒数并将该值标准化:
class Clock:
def __init__(self, h, m):
# store seconds, but only within the range of a day
self.seconds = (h * 3600 + m * 60) % 86400
self.adl = 0
def make_time(self):
s = self.esconds
if self.adl: s += self.adl
s %= 86400
if s == 0:
return '00:00'
s, h = s % 3600, s // 3600
m = s // 60
return '{:02d}:{:02d}'.format(h, m)
def __eq__(self, other):
if not isinstance(other, Clock):
return NotImplemented
return (self.seconds, self.adl) == (other.seconds, other.adl)
现在您的两个时钟实例将测试相等,因为它们在内部存储一天中完全相同的时间。请注意,我使用了 %
模数运算符而不是 while
循环和减法。
我正在做 exorcism.io 时钟练习,我不明白为什么这个测试失败了。结果看起来完全相同,甚至具有相同的类型。
这是我的代码:
class Clock:
def __init__(self, h, m):
self.h = h
self.m = m
self.adl = 0
def make_time(self):
s = self.h * 3600
s += self.m * 60
if self.adl: s += self.adl
while s > 86400:
s -= 86400
if s == 0:
return '00:00'
h = s // 3600
if h:
s -= h * 3600
m = s // 60
return '{:02d}:{:02d}'.format(h, m)
def add(self, more):
self.adl = more * 60
return self.make_time()
def __str__(self):
return str(self.make_time()) # i don't think I need to do this
if __name__ == '__main__':
cl1 = Clock(34, 37) #10:37
cl2 = Clock(10, 37) #10:37
print(type(cl2))
print(cl2, cl1)
print(cl2 == cl1) #false
没有 __eq__
method 的自定义 class 默认测试 身份 。也就是说,仅当引用完全相同的对象时,对此类 class 实例的两个引用才相等。
您需要定义一个自定义 __eq__
方法,当两个实例包含相同时间时 returns True
:
def __eq__(self, other):
if not isinstance(other, Clock):
return NotImplemented
return (self.h, self.m, self.adl) == (other.h, other.m, other.adl)
通过为非 Clock
实例(或子 class)返回 NotImplemented
单例,您让 Python 知道 other
对象也可以被要求测试是否相等。
但是,您的代码接受大于正常小时和分钟范围的值;与其存储小时和分钟,不如存储秒数并将该值标准化:
class Clock:
def __init__(self, h, m):
# store seconds, but only within the range of a day
self.seconds = (h * 3600 + m * 60) % 86400
self.adl = 0
def make_time(self):
s = self.esconds
if self.adl: s += self.adl
s %= 86400
if s == 0:
return '00:00'
s, h = s % 3600, s // 3600
m = s // 60
return '{:02d}:{:02d}'.format(h, m)
def __eq__(self, other):
if not isinstance(other, Clock):
return NotImplemented
return (self.seconds, self.adl) == (other.seconds, other.adl)
现在您的两个时钟实例将测试相等,因为它们在内部存储一天中完全相同的时间。请注意,我使用了 %
模数运算符而不是 while
循环和减法。