3 ms·
As someone who's used Python 2 for the past half decade and started using Python 3 only recently, 100% with you on this. The first time I was working on a proj
by joatmon-snoo 10y ago
As someone who's used Python 2 for the past half decade and started using Python 3 only recently, 100% with you on this.
The first time I was working on a project which required me to be able to work with characters beyond ASCII, I was so confused. To this date, I'm still not sure if I can actually explain what str.encode and str.decode do in Python 2.
Admittedly I've never used Python 3 for a project where I had to deal with international charsets - and his point about the concatenation of unicode and bytearrays certainly seems a valid point - but it still seems infinitely better than what Python 2 did.
- kqr 10y ago> his point about the concatenation of unicode and bytearrays certainly seems a valid point This is one of those subjects where I feel like being a non-English programmer has given me surprisingly valuable experience. The operation of concatenating a text value (string) and binary data (byte array) simply does not make sense, for two reasons. What do you expect the result to be? 1. Binary data with the text appended. Then you need to specify which encoding you want the text to have. Once you do (a.encode('utf-8')), all will be fine. You could argue that a conversion to utf-8 should be implicit since that's almost always the correct choice, but you'd be surprised how fast you run into trouble with other systems. 2. Text with the binary data appended after interpreting it as text. Again, which encoding? You'll get very confusing and terrible results if you do not specify this. If you specify this (b.decode('utf-8')) everything will be fine. The only other alternative I accept is a type error. I'm guessing zed would be even angrier if that was the case. I'm not quite sure what Python 3 does, but at least it somewhat communicates that whatever interpretation it does is internal and arbitrary. The Python 2 result is ridiculous.
- pwdisswordfish 10y agoFor the record: Python 3.5.2 (default, Nov 7 2016, 11:31:36) [GCC 6.2.1 20160830] on linux Type "help", "copyright", "credits" or "license" for more information. >>> u'' + b'' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't convert 'bytes' object to str implicitly >>> u'' + bytearray() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't convert 'bytearray' object to str implicitly And I agree wholeheartedly. This is the only sane thing to do.
- joatmon-snoo 10y agoThat's a pretty fair point, and I completely agree that the Python 2 result is pretty ridiculous. In zed's defense, it still does seem like a rather daunting error for a beginner programmer - although like you, I have no good viable alternative to suggest.
- kqr 10y agoEncoding and decoding binary data is hard for a beginner in general, and I'd argue it's good to avoid it for the first few lessons. Unless the beginner has a CS education in which case by all means explain how encoding is taking text and translating it to bits.