我有一个简单的课程python /questions/tagged/python:
class simple(object):
def __init__(self, theType, someNum):
self.theType = theType
self.someNum = someNum
后来在我的程序中,我创建了该类的多个实例,即:
a = simple('A', 1)
b = simple('A', 2)
c = simple('B', 3)
d = simple('B', 4)
e = simple('C', 5)
allThings = [a, b, c, d, e] # Fails "areAllOfSameType(allThings)" check
a = simple('B', 1)
b = simple('B', 2)
c = simple('B', 3)
d = simple('B', 4)
e = simple('B', 5)
allThings = [a, b, c, d, e] # Passes "areAllOfSameType(allThings)" check
我需要测试中的所有元素是否allThings
simple.theType 具有相同的值。我将如何为此编写通用测试,以便将来可以包含新的“类型”(即D
, E
, F
等)而不需要重写我的测试逻辑?我可以想出一种通过直方图来做到这一点的方法,但我认为有一种“Pythonic”方法可以做到这一点。