> On 3 Nov 2019, at 12:17, Steven D'Aprano <st...@pearwood.info> wrote:
> 
> On Sun, Nov 03, 2019 at 11:31:22AM +0100, Anders Hovmöller wrote:
> 
>>> Given:
>>>  values = ["Hello", b"World"]
>>> would you expect ' '.join(values) to return
>>>  "Hello b'World'"
>>> because that's what you'll get calling str automatically.
>> Side note!
>> That misfeature of python 3 was one of the largest source of
>> continuing bugs in production for us after we switched.
> 
> Misfeature? If you called it a "gotcha", I'd agree with your 
> description.
> 
> We have two mutually exclusive requirements:
> 
>   1. You should be able to coerce *anything* and *everything* into
>   a string, no exceptions. Round tripping is not required, but
>   print(x) should never fail, it should always print something,
>   and that requires that str(x) should work for all x.

Well, the last part requires that print can coerce bytes to str, not strictly 
that it must do so via str(). repr() should return b'foo' but str() could have 
returned <bytes object at 0x6363737>. 

>   2. We shouldn't be able to turn bytes into a str without 
>   specifying the correct encoding to use.

Except that's exactly what str(bytes) does but with garbage at the start and 
end. That's how all the bugs manifested themselves at least. It was especially 
aggravating because 100% of the bugs we hit the bytes were ascii. 

> Clearly we can't have both requirements, we have to choose one or the 
> other. The compromise solution was to change requirement 2 to be 
> something like:
> 
>   2a. We shouldn't be able to DECODE bytes into a str without 
>   specifying the correct encoding to use (using the decode method).
> 
> and allow str(bytes) with no encoding specified to just return a 
> representation of the bytes object. Like all compromises, nobody is 
> really happy about it, but some people are probably more unhappy than 
> others.
> 
> While I sympathise with your pain, its hard to see that any other 
> solution would have been better.

I think it's more fair to say it's hard to have understood the problem 
sufficiently a priori enough to come up with a better solution at the time. But 
here after the fact one can come up with better solutions. 

It's mostly a problem just when switching between 2 and 3 so for me personally 
this problem isn't relevant anymore. We'll see if it becomes relevant again. I 
might be roped into moving another huge code base to python 3 soonish though :/

/ Anders 
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/U5CIL64FW7OVWQ36PBPPF64VVHH4J6XY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to