This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: bytes class extension with slices
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Sec42, serhiy.storchaka
Priority: normal Keywords:

Created on 2022-02-26 11:37 by Sec42, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bytes_test.py Sec42, 2022-02-26 11:37 test case
Messages (2)
msg414092 - (view) Author: Sec (Sec42) Date: 2022-02-26 11:37
When trying to extend the builtin bytes class, slices fall back to the builtin class.

```
class my_bytes(bytes):
  def dummy(self):
    print("dummy called")


x=my_bytes.fromhex("c0de c0de")
print(x.__class__)

print(x[1:].__class__)

```

x.__class__ returns <class '__main__.my_bytes'> as expected.

But x[1:].__class__ returns <class 'bytes'>
msg414095 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2022-02-26 12:08
Yes, it is consistent with all of builtin types. If you want to return a different type, override __getitem__().
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 91022
2022-02-26 12:08:24serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg414095

resolution: not a bug
stage: resolved
2022-02-26 11:37:49Sec42create