<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Sat, Aug 6, 2016 at 8:45 PM INADA Naoki <<a href="mailto:songofacandy@gmail.com">songofacandy@gmail.com</a>> wrote:</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
1. bytes(bytearray[:n])<br>
2. bytes(memoryview(bytearray)[:n])<br>
<br>
(1) is simplest, but it produces temporary bytearray having n bytes.<br></blockquote><div><br></div><div>Does that actually make the difference between unacceptably inefficient performance and acceptably efficient for an application you're working on?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
While (2) is more efficient than (1), it uses still temporary memoryview<br>
object, and it looks bit tricky.<br></blockquote><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">Using the memoryview is nicely explicit whereas ``bytes.frombuffer`` could be creating a temporary bytearray as part of its construction.</span><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The API I propose looks like this:<br>bytes.frombuffer(byteslike, length=-1, offset=0)<br></blockquote><div><br></div><div>RawIOBase.read and the other read methods described in the io module use the parameter "size" instead of "length".</div><div><a href="https://docs.python.org/3/library/io.html#io.RawIOBase">https://docs.python.org/3/library/io.html#io.RawIOBase</a></div></div></div>