python空指针异常_Ironpython:函数在CPython中起作用,IronPython中的神秘空指针异常...

I'm trying to do something that seems very simple, and falls within the range of standard python. The following function takes a collection of sets, and returns all of the items that are contained in two or more sets.

To do this, while the collection of sets is not empty, it simply pops one set out of the collection, intersects it with the remaining sets, and updates a set of items that fall in one of these intersections.

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

Above is the function I'm using. To test it, in CPython, the following works:

>>> 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])

However, when I try to use 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.

In the title I said this was a mysterious null pointer exception. I probably have no idea how .NET handles null pointers (I've never worked with a C-like language, and have only been using IronPython for a month or so), but if my understanding is correct, it occurs when you attempt to access some property of an object that points to null.

In this case, the error occurs at line 10 of my function: in_two |= new. However, I've put print statements right before this line that (at least to me) indicate that neither of these objects point to null.

Where am I going wrong?

解决方案

It's a bug. It will be fixed in 2.7.1, but I don't think the fix is in the 2.7.1 Beta 1 release.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值