4

私は、非常に単純に見え、標準の 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 |= newprintただし、この行の直前に、(少なくとも私にとっては) これらのオブジェクトがいずれも を指していないことを示すステートメントを入れましたnull

どこが間違っていますか?

4

2 に答える 2

3

バグです。2.7.1 で修正される予定ですが、2.7.1 Beta 1 リリースでは修正されていないと思います。

于 2011-07-15T20:26:57.217 に答える
1

これは、2.7.1Beta1リリースにまだ存在するバグです。

これはmasterで修正されており、修正は次のリリースに含まれる予定です。

IronPython 3.0 (3.0.0.0) on .NET 4.0.30319.235
Type "help", "copyright", "credits" or "license" for more information.
>>> import copy
>>>
>>> def cross_intersections(sets):
...     in_two = set()
...     sets_copy = 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
...
>>>
>>> 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])
于 2011-07-15T21:46:54.373 に答える