gpt4 book ai didi

python - 声明的列表引发 NoneType 异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:12 25 4
gpt4 key购买 nike

我有一个来自其他人的 python 模块的片段,它引发了一个异常(AttributeError: 'NoneType' object has no attribute 'append')。片段是:

def generateFragments(self):
tr = self.deviceTransform()
if tr is None:
return
pts = np.empty((2,len(self.data['x'])))
pts[0] = self.data['x']
pts[1] = self.data['y']
pts = fn.transformCoordinates(tr, pts)
self.fragments = []
pts = np.clip(pts, -2**30, 2**30) ## prevent Qt segmentation fault.
## Still won't be able to render correctly, though.
for i in xrange(len(self.data)):
rec = self.data[i]
pos = QtCore.QPointF(pts[0,i], pts[1,i])
x,y,w,h = rec['fragCoords']
rect = QtCore.QRectF(y, x, h, w)
self.fragments.append(QtGui.QPainter.PixmapFragment.create(pos, rect))

异常信息是:

[14:35:59]  Ignored exception:

|==============================>>
| Traceback (most recent call last):
| File "/usr/lib/pymodules/python2.7/pyqtgraph/debug.py", line 35, in w
| func(*args, **kwds)
| File "/usr/lib/pymodules/python2.7/pyqtgraph/graphicsItems/ScatterPlotItem.py", line 714, in paint
| self.generateFragments()
| File "/usr/lib/pymodules/python2.7/pyqtgraph/graphicsItems/ScatterPlotItem.py", line 685, in generateFragments
| self.fragments.append(QtGui.QPainter.PixmapFragment.create(pos, rect))
| AttributeError: 'NoneType' object has no attribute 'append'
|
|==============================<<

这不是“修复我的错误”问题,而是“这怎么可能”的问题。上面几行,self.fragments 被声明为一个空列表。从那时起,self.fragments 的唯一修改就是调用 append()。类型怎么可能改变了?追加操作是否可能失败,可能会将列表的类型更改为 NoneType

最佳答案

我想到的两种可能性是:

  • 并发性(根据@ekhumoro 的评论)。
  • 该类正在定义一个 __getattribute__ 方法,该方法使通常的属性检索机制短路。

关于python - 声明的列表引发 NoneType 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26601053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com