【发布时间】:2017-08-30 00:20:19
【问题描述】:
我在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”方法可以做到这一点。
【问题讨论】:
标签: python python linux unique membership