Blame
|
1 | # Bytes Bytearray |
||||||
| 2 | bytes/bytearray can contain numbers between 0 and 255 |
|||||||
| 3 | ||||||||
| 4 | ## Overview |
|||||||
| 5 | | What | How | |
|||||||
| 6 | | - | - | |
|||||||
| 7 | | `bytes` | immutable byte sequence | |
|||||||
| 8 | | `bytes(10)` | create bytes of length 10 | |
|||||||
| 9 | | `bytes[0]` | get first byte | |
|||||||
| 10 | | - | - | |
|||||||
| 11 | | `bytearray` | mutable byte sequence | |
|||||||
| 12 | | `bytearray(10)` | create bytearray of length 10 | |
|||||||
| 13 | | `bytearray[0]` | get first byte | |
|||||||
| 14 | | - | - | |
|||||||
| 15 | | *bytes from bytearray* | `bytes(bytearray)` | |
|||||||
| 16 | | *bytearray from bytes* | `bytearray(bytes)` | |
|||||||
| 17 | | *copy from bytearrayOrBytes* | `bytearray(bytearrayOrBytes)` | |
|||||||
| 18 | ||||||||
| 19 | handle both with one method: |
|||||||
| 20 | ```python |
|||||||
| 21 | def method(data: bytearray | bytes): # accept both types |
|||||||
| 22 | mydata = bytearray(data) # copy of bytes or bytearray |
|||||||
| 23 | ``` |
|||||||
