我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
等),而不必重新編寫我的測試邏輯是什麼?我能想到的方式通過直方圖來做到這一點,但我想有一個「Python化」的方式來做到這一點。