[New-bugs-announce] [issue24892] bytes.join() won't take it's own type as the argument

Timothy Geiser report at bugs.python.org
Wed Aug 19 00:37:47 CEST 2015


New submission from Timothy Geiser:

You can't join bytes on another bytes object. (Everything below applies to bytearray, as well)


>>> x = b'foo'
>>> y = b'barbaz'
>>> x.join(y)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    x.join(y)
TypeError: sequence item 0: expected a bytes-like object, int found


But this is fine for strings, and gives you exactly what you'd expect


>>> x = 'foo'
>>> y = 'barbaz'
>>> x.join(y)
'bfooafoorfoobfooafooz'
>>> y.join(x)
'fbarbazobarbazo'


The best work-around I could think of was


>>> x = b'foo'
>>> y = b'barbaz'
>>> x.join(y[i:i+1] for i in range(len(y)))
b'bfooafoorfoobfooafooz'
>>> y.join(x[i:i+1] for i in range(len(x)))
b'fbarbazobarbazo'


That just doesn't feel nearly pythonic enough, considering that the string version works as expected. I'm not even sure what the right solution here is, since the problem is that the iterator for a bytes object returns ints, not length-one bytes objects. Do we need another signature for the bytes.join method that takes byte-like objects and does what I'm describing here?

----------
components: Interpreter Core
messages: 248799
nosy: geitda
priority: normal
severity: normal
status: open
title: bytes.join() won't take it's own type as the argument
type: enhancement
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24892>
_______________________________________


More information about the New-bugs-announce mailing list