You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bpo-24234: Should we define complex.complex and bytes.bytes?
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=<Date2018-03-29.00:04:41.868>created_at=<Date2018-03-12.14:28:02.041>labels= ['interpreter-core', '3.8', 'type-bug', '3.7']
title='bytes does not implement __bytes__()'updated_at=<Date2018-03-29.00:04:41.867>user='https://github.com/FHTMitchell'
This isn't one exception. For example complex doesn't have the __complex__ method.
__str__, __float__, __int__ and __bool__ are implemented in str, float, int and bool because there are corresponding special slots in a type object. But __bytes__ and __complex__ were added later and they have no slots. Calling them for bytes and complex will slowdown creating new object.
The user code rarely needs to call these methods directly. Just call bytes(), complex(), str(), float(), etc.
It is against Guido's general policy to add things not needed purely for consistency. Unless there is a compelling use case, I think this should be closed.
FHTM: I don't think Serhiy posts on SO, so you might consider adding his answer to yours, or make it a separate answer.
Activity
FHTMitchell commentedon Mar 12, 2018
Every object which has a corresponding dunder protocol also implements said protocol with one exception:
AttributeError: 'bytes' object has no attribute '__bytes__'
This was brought up on SO as being inconsistent: https://stackoverflow.com/questions/49236655/bytes-doesnt-have-bytes-method/49237034?noredirect=1#comment85477673_49237034
serhiy-storchaka commentedon Mar 12, 2018
This isn't one exception. For example complex doesn't have the __complex__ method.
__str__, __float__, __int__ and __bool__ are implemented in str, float, int and bool because there are corresponding special slots in a type object. But __bytes__ and __complex__ were added later and they have no slots. Calling them for bytes and complex will slowdown creating new object.
The user code rarely needs to call these methods directly. Just call bytes(), complex(), str(), float(), etc.
terryjreedy commentedon Mar 16, 2018
It is against Guido's general policy to add things not needed purely for consistency. Unless there is a compelling use case, I think this should be closed.
FHTM: I don't think Serhiy posts on SO, so you might consider adding his answer to yours, or make it a separate answer.
serhiy-storchaka commentedon Mar 29, 2018
In any case this is a duplicate of 24234