Python 创建自定义对象的 numpy 数组会出现错误“SystemError:错误返回而未设置异常”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20482837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:34:18  来源:igfitidea点击:

Creating numpy array of custom objects gives error "SystemError: error return without exception set"

pythonnumpy

提问by Dannnno

I am trying to use numpy to store some custom objects I've made. The following is a simplified version of my program

我正在尝试使用 numpy 来存储我制作的一些自定义对象。以下是我的程序的简化版

import numpy as np

class Element:
    def __init__(self): pass

a = Element()
periodicTable = np.array(range(7*32)).reshape((7,32))
periodicTable[0][0] = a

However when I run this I get

但是,当我运行它时,我得到

Traceback (most recent call last):
  File "C:/Users/Dan/Desktop/a.py", line 9, in <module>
    periodicTable[0][0] = a
SystemError: error return without exception set

I'm not really sure what I'm doing wrong - as far as I can tell everything I've done should be legal. The cryptic error message itself isn't very helpful - I believe that is a numpy issue however I've been unable to identify my problem.

我不确定我做错了什么——据我所知,我所做的一切都应该是合法的。神秘的错误消息本身并不是很有帮助 - 我相信这是一个很棘手的问题,但是我一直无法确定我的问题。

采纳答案by Warren Weckesser

@user2357112 identified the problem: you are assigning an Elementinstance to a numpy array that holds integers. This is what I get when I try something similar:

@user2357112 发现了问题:您正在将一个Element实例分配给一个包含整数的 numpy 数组。当我尝试类似的东西时,这就是我得到的:

>>> import numpy as np
>>> np.__version__
'1.7.1'
>>> p = np.array([1,2,3])
>>> class Foo:
...     pass
... 
>>> p[0] = Foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SystemError: error return without exception set
>>> 

It is not surprising that this is not allowed. The cryptic error message, however, is almost certainly a numpy bug.

这是不允许的,这并不奇怪。然而,这个神秘的错误信息几乎可以肯定是一个 numpy 错误。

One way to fix the issue is to use an array of type object. Change this line:

解决此问题的一种方法是使用类型为 的数组object。改变这一行:

    periodicTable = np.array(range(7*32)).reshape((7,32))

to this:

对此:

    periodicTable = np.empty((7,32), dtype=object)


Update

更新

In numpy 1.10.1, the error message is still a bit cryptic:

在 numpy 1.10.1 中,错误信息仍然有点神秘:

>>> import numpy as np
>>> np.__version__
'1.10.1'    
>>> p = np.array([1, 2, 3])
>>> class Foo:
...     pass
...  
>>> p[0] = Foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Foo instance has no attribute '__trunc__'


Update 2

更新 2

The error message is better is later versions of numpy:

错误信息更好的是 numpy 的更高版本:

In [1]: import numpy as np

In [2]: np.__version__
Out[2]: '1.12.1'

In [3]: class Foo:
   ...:     pass
   ...: 

In [4]: p = np.array([1, 2, 3])

In [5]: p[0] = Foo()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-739d5e5f795b> in <module>()
----> 1 p[0] = Foo()

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Foo'