即使在 class 属性中满足要求也会引发 AssertionError?
AssertionError raised even when requirements are met in a class attribute?
我有一个 class 间隔 class 属性 compare_mode
设置为 None
。 compare_mode 用于关系运算符,比较基于不同“模式”的值。
当 compare_mode
是 'liberal'
或 'conservative'
时,它应该转到 __lt__
中它们各自的 if-else 语句并进行比较,但即使我已经将 compare_mode
设置为 'liberal'
或 'conservative'
,AssertionError
在 __lt__
中被提升,就好像值仍然是 None
?
我不确定这里发生了什么。任何解释将不胜感激。
class Interval:
compare_mode = None
def __init__(self, mini, maxi):
self.mini = mini
self.maxi = maxi
@staticmethod
def min_max(mini, maxi = None):
assert isinstance(mini, (int,float)), "Minimum value must be an int or float"
if maxi != None:
assert isinstance(maxi, (int,float)), "Maximum value must be an int, float, or None"
if maxi != None and mini > maxi:
raise AssertionError('Minimum value is greater than the maximum value')
elif maxi == None and isinstance(mini, (int, float)):
maxi = mini
return Interval(mini, maxi)
def __lt__(self, other):
if Interval.compare_mode != 'liberal' or Interval.compare_mode != 'conservative':
raise AssertionError
if not isinstance(other, (int, float, Interval)):
return NotImplemented
if Interval.compare_mode == 'liberal':
if isinstance(other, (int,float)):
return ((self.mini + self.maxi) / 2) < other
else:
return ((self.mini + self.maxi) / 2) < ((other.mini + other.maxi) / 2)
elif Interval.compare_mode == 'conservative':
if isinstance(other, (int,float)):
return self.maxi < other
else:
return self.maxi < other.mini
if __name__ == '__main__':
l = Interval.min_max(1.0,5.0)
r = Interval.min_max(4.0,6.0)
e = Interval.min_max(4.0,4.0)
Interval.compare_mode = 'liberal'
print(l<l)
>>> AssertionError:
将您的条件更改为
Interval.compare_mode != 'liberal' and Interval.compare_mode != 'conservative'
我有一个 class 间隔 class 属性 compare_mode
设置为 None
。 compare_mode 用于关系运算符,比较基于不同“模式”的值。
当 compare_mode
是 'liberal'
或 'conservative'
时,它应该转到 __lt__
中它们各自的 if-else 语句并进行比较,但即使我已经将 compare_mode
设置为 'liberal'
或 'conservative'
,AssertionError
在 __lt__
中被提升,就好像值仍然是 None
?
我不确定这里发生了什么。任何解释将不胜感激。
class Interval:
compare_mode = None
def __init__(self, mini, maxi):
self.mini = mini
self.maxi = maxi
@staticmethod
def min_max(mini, maxi = None):
assert isinstance(mini, (int,float)), "Minimum value must be an int or float"
if maxi != None:
assert isinstance(maxi, (int,float)), "Maximum value must be an int, float, or None"
if maxi != None and mini > maxi:
raise AssertionError('Minimum value is greater than the maximum value')
elif maxi == None and isinstance(mini, (int, float)):
maxi = mini
return Interval(mini, maxi)
def __lt__(self, other):
if Interval.compare_mode != 'liberal' or Interval.compare_mode != 'conservative':
raise AssertionError
if not isinstance(other, (int, float, Interval)):
return NotImplemented
if Interval.compare_mode == 'liberal':
if isinstance(other, (int,float)):
return ((self.mini + self.maxi) / 2) < other
else:
return ((self.mini + self.maxi) / 2) < ((other.mini + other.maxi) / 2)
elif Interval.compare_mode == 'conservative':
if isinstance(other, (int,float)):
return self.maxi < other
else:
return self.maxi < other.mini
if __name__ == '__main__':
l = Interval.min_max(1.0,5.0)
r = Interval.min_max(4.0,6.0)
e = Interval.min_max(4.0,4.0)
Interval.compare_mode = 'liberal'
print(l<l)
>>> AssertionError:
将您的条件更改为
Interval.compare_mode != 'liberal' and Interval.compare_mode != 'conservative'