str() should convert ANY object to a string without EXCEPTIONS !

Christian Heimes lists at cheimes.de
Sun Sep 28 07:34:44 EDT 2008


Steven D'Aprano wrote:
>>>> str(b'123') # b prefix is added
> "b'123'"
> 
> 
> Perhaps I'm misinterpreting it, but from here it looks to me that str() 
> is doing what repr() used to do, and I'm really not sure that's a good 
> thing. I would have expected that str(b'123') in Python 3 should do the 
> same thing as unicode('123') does now:

No, you are getting it right and yes, it's problematic. Guido wanted 
str(b'') to succeed. But the behavior can easily mask bugs in code. 
Therefor a byte warning mode was implemented.


$ ./python -b
 >>> str(b'123')
__main__:1: BytesWarning: str() on a bytes instance
"b'123'"

$ ./python -bb
 >>> str(b'123')
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
BytesWarning: str() on a bytes instance
 >>> b'' == ''
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
BytesWarning: Comparison between bytes and string




More information about the Python-list mailing list