私は、非常に単純に見え、標準の python の範囲内にあることをしようとしています。次の関数は、セットのコレクションを受け取り、2 つ以上のセットに含まれるすべての項目を返します。
これを行うには、セットのコレクションは空ではありませんが、コレクションから 1 つのセットをポップし、残りのセットと交差させ、これらの交差のいずれかに該当するアイテムのセットを更新するだけです。
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 で次のように動作します。
>>> a = set([1,2,3,4])
>>> b = set([3,4,5,6])
>>> c = set([2,4,6,8])
>>> cross = cross_intersections([a,b,c])
set([2, 4]) set([])
set([4, 6]) set([2, 4])
set([3, 4]) set([2, 4, 6])
>>> cross
set([2, 3, 4, 6])
ただし、IronPython を使用しようとすると:
>>> b = cross_intersections([a,b,c])
set([2, 4]) set([])
set([4, 6]) set([2, 4])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:/path/to/code.py", line 10, in cross_intersections
SystemError: Object reference not set to an instance of an object.
タイトルで、これは不思議な null ポインター例外だと言いました。.NET がヌル ポインターをどのように処理するかはおそらくわかりません (C のような言語を使用したことがなく、IronPython を使用したのは 1 か月程度です) が、私の理解が正しければ、を指すオブジェクトのプロパティにアクセスしますnull
。
この場合、エラーは my function: の 10 行目で発生しますin_two |= new
。print
ただし、この行の直前に、(少なくとも私にとっては) これらのオブジェクトがいずれも を指していないことを示すステートメントを入れましたnull
。
どこが間違っていますか?