-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Slicing a bytes object returning <int> with single index #132062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It’s not actually, strings are the weird ones. Consider a list - |
Are you about this: offset = struct.unpack_from("<b", offset[0])[0] # offset[0] ? It's not slicing, but subscription. |
Call it how you like, indexing, subscription or slicing-of-one-item: but one expects that what you extract is what there is inside an object - bytes -> byte |
Python works here as expected, similar you can see just with ordinary lists: >>> xs = [1, 2, 3]
>>> type(xs[0])
<class 'int'>
>>> type(xs[0:1])
<class 'list'> Problem is definitely with your expectations. |
It is documented behavior since 3.3:
|
In fact, that was documented since beginning in py3, see 98297ee. |
Bug report
Bug description:
Consider the first 'if' in the following piece of code.
It used to run nice in Python 2.x (where it was developed):
Actually, it raises the exception
TypeError: a bytes-like object is required, not 'int'
and needs an awkward reconversion:Isn't it a contradiction (and a bug) that slicing returns bytes except in the case of a single slicing index???
CPython versions tested on:
3.13
Operating systems tested on:
Windows
The text was updated successfully, but these errors were encountered: