Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper python way to clear a bytearray

I miss memset. I have a frame buffer in a class, it has data in it from some other operations, and now I want to clear everything in that frame buffer to 0x00 or 0xFF. I didn't see a clear method in the docs, there's a zfill method that might work. I thought about just calling the init method of the byte array again but wondered if that might cause me some memory trouble down the road.

I'm using python 2.7.

like image 871
confused Avatar asked Apr 15 '16 17:04

confused


People also ask

How do you create an empty Bytearray in Python?

The bytearray() function returns a bytearray object. It can convert objects into bytearray objects or create an empty bytearray object of the specified size. Some rules are followed by the bytearray() function depending on the source type. If no argument is passed, an empty byte array is returned.

What is Bytearray in Python?

Python bytearray() Function The bytearray() function returns a bytearray object. It can convert objects into bytearray objects, or create empty bytearray object of the specified size.

What does Bytearray return Python?

bytearray() method returns a bytearray object which is an array of given bytes. It gives a mutable sequence of integers in the range 0 <= x < 256. Returns: Returns an array of bytes of the given size. source parameter can be used to initialize the array in few different ways.


1 Answers

Python is not C, here you don't need to worry about memory at all much. You just simply do:

a = bytearray("abcd") # initialized it with real data

a = bytearray(5) # just a byte array of size 5 filled with zeros
like image 86
ForceBru Avatar answered Nov 06 '22 18:11

ForceBru