gpt4 book ai didi

python - numpy.getbuffer 导致 AttributeError : 'module' object has no attribute 'getbuffer'

转载 作者:太空狗 更新时间:2023-10-29 21:31:46 26 4
gpt4 key购买 nike

我想从 Python 3 中的 numpy 数组中获取缓冲区。我找到了以下代码:

$ python3
Python 3.2.3 (default, Sep 25 2013, 18:25:56)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> a = numpy.arange(10)
>>> numpy.getbuffer(a)

但是它在最后一步产生了错误:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getbuffer'

为什么我做错了?该代码适用于 Python 2。我使用的 numpy 版本是 1.6.1。

最佳答案

根据 Developer notes on the transition to Python 3 :

PyBuffer (object)

Since there is a native buffer object in Py3, the memoryview, the newbuffer and getbuffer functions are removed from multiarray in Py3: their functionality is taken over by the new memoryview object.

>>> import numpy
>>> a = numpy.arange(10)
>>> memoryview(a)
<memory at 0xb60ae094>
>>> m = _
>>> m[0] = 9
>>> a
array([9, 1, 2, 3, 4, 5, 6, 7, 8, 9])

关于python - numpy.getbuffer 导致 AttributeError : 'module' object has no attribute 'getbuffer' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21821045/

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