我写了这段代码:
def func(collection_type):
assert isinstance(collection_type,(list,set))
然后我写了:
func(collection_type=set)
我收到了AssertionError
答案 0 :(得分:0)
assert isinstance(collection_type,(list,set))
。这将测试给定的实例是否属于(list,set)
。因此,尝试使用list或set这样的实例,
func(collection_type=[1, 2, 3])
或func(collection_type={1, 2, 3})
。
在python中,所有类都是type
类的实例。因此,如果您像func(collection_type=set)
这样传递类本身。它将检查type
,因为仅存在(list,set)
,它将引发assertionError。
如果要使用空集进行测试,请尝试func(collection_type=set())