← index #3158PR #6457
Off-topic · high · value 2.383
QUERY · ISSUE

py: bytes() won't call obj.__bytes__()

openby ThunderEXopened 2017-06-21updated 2024-08-28
docs

I defined a class with bytes method. Calling bytes() on its instance should redirect to its bytes method, but it doesn't.

Can this be fixed?

CANDIDATE · PULL REQUEST

py/objstr: bytes(bytes_obj) is bytes_obj

closedby iyassouopened 2020-09-18updated 2020-09-24
py-core

Calling the bytes constructor on a bytes object returns the original bytes object. This addresses #6320 and has been tested on an ESP32, and everything seems to be working fine:

MicroPython v1.13-dirty on 2020-09-18; ESP32 module with ESP32
Type "help()" for more information.
>>> a = b"Original"
>>> b = a
>>> c = bytes(a)
>>> type(a)
<class 'bytes'>
>>> a is b
True
>>> a is c
True
>>> id(a)
1073642736
>>> id(b)
1073642736
>>> id(c)
1073642736

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