Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Example 2-21 in book errors out in python 2.7 #11

@robynsmith

Description

@robynsmith
# Example 2-21. Changing the value of an array item by poking one of its bytes
import array
numbers = array.array('h', [-2, -1, 0, 1, 2])
memv = memoryview(numbers)

Gives me this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-f1be3db9ba34> in <module>()
----> 1 memv = memoryview(numbers)

TypeError: cannot make memory view because object does not have the buffer interface

Based on research I did, this is a known limitation of python 2.7:

An array can only expose its contents via the old-style buffer interface. This limitation does not apply to Python 3, where memoryview objects can be constructed from arrays, too. Array elements may be multi-byte values.

Source: Python Docs

This info might be useful to someone out there. Not sure if it's worth noting in a future revision of the book :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions