Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67518

Re: how to get bytes from bytearray without copying

From Juraj Ivančić <juraj.ivancic@gmail.com>
Subject Re: how to get bytes from bytearray without copying
Date 2014-03-03 09:52 +0100
References <lf0h3g$m7r$1@ger.gmane.org> <lf0jif$dpp$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.7629.1393836762.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 3.3.2014. 1:49, Mark Lawrence wrote:

> If your data is readonly why can't you simply read it as bytes in the
> first place?  Failing that from
> http://docs.python.org/3/library/stdtypes.html#memoryview
>
> tobytes() - Return the data in the buffer as a bytestring. This is
> equivalent to calling the bytes constructor on the memoryview.
>
>   >>> m = memoryview(b"abc")
>   >>> m.tobytes()
> b'abc'
>   >>> bytes(m)
> b'abc'

Initially it has to be a bytearray because I read this data from a 
socket. My point is that once I have a bytearray x, then

m = memoryview(bytes(x))

is a very expensive way to make a read-only memoryview, opposed to

m = memoryview(x)

or (fictional)

m = memoryview(x, force_readonly=True)

especially if the x-es are many, large, and occur often.

I feel like memoryview's __hash__ is trying to be to smart for its own 
good, and that it should just return the damn hash like its name 
suggests, regardless of the value of 'writable' flag.





Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: how to get bytes from bytearray without copying Juraj Ivančić <juraj.ivancic@gmail.com> - 2014-03-03 09:52 +0100

csiph-web