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


Groups > comp.lang.python > #67518 > unrolled thread

Re: how to get bytes from bytearray without copying

Started byJuraj Ivančić <juraj.ivancic@gmail.com>
First post2014-03-03 09:52 +0100
Last post2014-03-03 09:52 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#67518 — Re: how to get bytes from bytearray without copying

FromJuraj Ivančić <juraj.ivancic@gmail.com>
Date2014-03-03 09:52 +0100
SubjectRe: how to get bytes from bytearray without copying
Message-ID<mailman.7629.1393836762.18130.python-list@python.org>
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.





[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web