← index #1710Issue #1424
Related · high · value 2.785
QUERY · ISSUE

bytearray slice assignment fails for derived types

openby mbueschopened 2015-12-11updated 2024-08-28
docs

The type check for slice assignment to bytearray derived types seems to be a bit too strict:

class MyBytearray(bytearray): pass

a = MyBytearray(4)
b = MyBytearray(4)
a[0:4] = b

results in this error:

Traceback (most recent call last):
  File "test.py", line 5, in <module>
NotImplementedError: array/bytes required on right side
CANDIDATE · ISSUE

bytearray slice assignment from generator produces error

closedby peterhinchopened 2015-08-13updated 2022-06-25

Is this an intentional "micro" difference from Cpython or an oversight? This works in Cpython but produces a "Not implemented" error in MicroPython (on Pyboard).

b = bytearray(20)
b[2:7] = (1 for x in range(5))

While the following workround is trivial I thought it worth flagging up:

b[2:7] = bytes(1 for x in range(5))

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied