is python 3 better than python 2?
tjreedy at udel.edu
Wed Apr 6 19:01:42 CEST 2011
On 4/6/2011 12:31 PM, MRAB wrote:
> On 06/04/2011 07:06, Dan Stromberg wrote:
>> I suspect not all string methods were kept for the bytes type:
Doc says "Bytes and bytearray objects, being “strings of bytes”, have
all methods found on strings, with the exception of encode(), format()
and isidentifier(), which do not make sense with these types. " If you
find that in error, please file a bug report. You can check with
dir(str) and dir(bytes), possibly using set differences. But I believe I
did that once.
>> >>> 'a/b/c'.split('/')
>> ['a', 'b', 'c']
>> >>> b'a/b/c'.split('/')
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> TypeError: Type str doesn't support the buffer API
Known confusing error message, but notice that you did *not* get
AttributeError: 'bytes' object has no attribute 'split'
which is the error you get for random names. So .split was successfully
looked up and then called, and its execution gave the above message.
> You're trying to split bytes with a str (Unicode) argument. Try this:
> >>> b'a/b/c'.split(b'/')
> [b'a', b'b', b'c']
One change in 3.x is the elimination of automatic coercions between
bytes and (unicode) strings.
# TypeError: Can't convert 'bytes' object to str implicitly
Terry Jan Reedy
More information about the Python-list