<div dir="ltr"><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">Hi all,</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">So I'm pretty sure everyone here is familiar with how the "bytes" object works in Python 3. It acts mostly like a string, with the exception that 0-dimensional subscripting (var[idx]) returns an integer, not a bytes object - the integer being the ordinal number of the corresponding character.</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">However, 1-dimensional subscripting (var[idx1:idx2]) returns a bytes object. Example:</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    >>> a = b'hovercraft'</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    >>> a[0]</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    104</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    >>> a[4:8]</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    b'rcra'</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">Though this isn't exactly unexpected behavior (it's not possible to accidentally do 1-dimensional subscripting and expect an integer it's a different syntax), it's still a shame that it isn't possible to quickly and easily subscript an integer out of it. Following up from the previous example, The only way to get 493182234161465432041076 out of b'hovercraft' in a single expression is as follows:</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    list(__import__('itertools').accumulate((i for i in a), lambda x, y: (x << 8) + y))[-1]</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">Now, I'm not proposing changing the 1-dimensional subscripting syntax to return an integer - that would be backwards incompatible, tsk tsk! No, instead, I'm simply suggesting a method of bytes objects, which would do something like this (assume the method is called "subint"):</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    >>> a = b'hovercraft'</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    >>> a.subint(0, -1) # -1 is equivalent to len(a)</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">    493182234161465432041076</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">Much as I would think that such subscripting would deserve special syntax (perhaps bytes{idx1:idx2}), I don't think this special case is special enough to break the rules. So I'm sticking with the method idea.</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)">What are your thoughts?</div><div class="gmail_default" style="font-family:monospace,monospace;font-size:small;color:rgb(0,0,255)"><br></div><div><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div><font face="monospace, monospace"><span style="color:rgb(0,0,255)">Sincerely,</span><br></font></div><font color="#0000ff" face="monospace, monospace">Ken;</font><br></div></div></div></div></div></div></div></div></div>
</div>