我尝试做一些看起来非常简单的事情,并且属于标准python的范围。以下函数接受集合集合,并返回包含在两个或多个集合中的所有项。在
为此,虽然集合集合不是空的,它只需从集合中弹出一个集合,将其与其余集合相交,并更新位于其中一个交集中的一组项。在def cross_intersections(sets):
in_two = set()
sets_copy = copy(sets)
while sets_copy:
comp = sets_copy.pop()
for each in sets_copy:
new = comp & each
print new, # Print statements to show that these references exist
print in_two
in_two |= new #This is where the error occurs in IronPython
return in_two
上面是我使用的函数。要测试它,在CPython中,需要执行以下操作:
^{pr2}$
但是,当我尝试使用IronPython时:>>> b = cross_intersections([a,b,c])
set([2, 4]) set([])
set([4, 6]) set([2, 4])
Traceback (most recent call last):
File "", line 1, in
File "c:/path/to/code.py", line 10, in cross_intersections
SystemError: Object reference not set to an instance of an object.
在标题中我说这是一个神秘的空指针异常。我可能不知道.NET是如何处理空指针的(我从未使用过类似C的语言,而且只使用了一个月左右的IronPython),但是如果我的理解是正确的,当您试图访问指向null的对象的某些属性时,就会发生这种情况。在
在本例中,错误发生在函数的第10行:in_two |= new。但是,我在这一行前面放了print语句,它(至少对我来说)表明这两个对象都没有指向null。在
我哪里出错了?在