[Python-Dev] (no subject)

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Feb 11 06:45:40 CET 2015


Antoine Pitrou wrote:
>>>>bytearray(b"a") + b"bc"
> 
> bytearray(b'abc')
> 
>>>>b"a" + bytearray(b"bc")
> 
> b'abc'
> 
> It's quite convenient.

It's a bit disconcerting that the left operand wins,
rather than one of them being designated as the
"wider" type, as occurs with many other operations on
mixed types, e.g. int + float.

In any case, these seem to be special-case combinations.
It's not so promiscuous as to accept any old iterable
on the right:

 >>> b"a" + [1,2,3]
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: can't concat bytes to list
 >>> [1,2,3] + b"a"
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "bytes") to list

-- 
Greg


More information about the Python-Dev mailing list