3

tl;dr Given a Sequence of memoryview, how can I create a single bytes instance without creating intermediate bytes instances?

The naive approach creates many intermediary instances of bytes

def create_bytes(seq_mv: Sequence[memoryview]) -> bytes:
    data = bytes()
    for mv in seq_mv:
        data = data + bytes(mv)
    return data

The function create_bytes creates len(seq_mv) + 1 instances of bytes during execution. That is inefficient.
I want create_bytes to create one new bytes instance during execution.

2
  • 4
    Did you try bytes().join(seq_mv)? Commented Oct 11, 2020 at 4:24
  • Indeed, join is here in the docs, too. Thanks! Commented Oct 11, 2020 at 4:50

1 Answer 1

4

bytes as you got it, is an imutable object.

As the Tim Peters put in the comments, you can let Python create a single instance with all parts joined together with a single call to bytes().join(seq_mv).

If you need to perform any other operation on your data that would involve changing it in the way, you could be using the mutable bytearray instead- which not only gives you flexibility to change your object, but have all the advantages of mutable sequences.

You can then make a single conversion to bytes at the end of your function if the users of your function can't deal straight with a a bytearray (but maybe you can just return it directly):

def create_bytes(seq_mv: Sequence[memoryview]) -> bytes:
    data = bytearray()
    for mv in seq_mv:
        data.extend(mv)
    return bytes(data)

Or simply:

from functools import reduce

data = reduce(lambda data, mv: data.extend(mv), seq_mv, bytearray())
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, good use of reduce! This is a nice extended answer ;-) . Indeed, join here in the docs. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.