5 ms·
> The strings in Python 3 are very difficult to use for beginners. In an attempt to make their strings more "international" they turned them into difficult to u
by lisper 10y ago
> The strings in Python 3 are very difficult to use for beginners. In an attempt to make their strings more "international" they turned them into difficult to use types with poor error messages. Every time you attempt to deal with characters in your programs you'll have to understand the difference between byte sequences and Unicode strings.
The author has this exactly backwards. It is Python2 that makes the weird distinction between the string and the unicode type. Strings (and bytes) in Python3 are done (almost) right. (I say "almost" because Python bytes are constrained to be 8-bits wide, but that is not a serious problem.)
He also clearly doesn't understand Turing-completeness.
- joatmon-snoo 10y agoAs 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.
- pdonis 10y ago> Python bytes are constrained to be 8-bits wide ?? Aren't all bytes 8 bits wide?
- joatmon-snoo 10y agoNope! (TIL) http://stackoverflow.com/questions/5516044/system-where-1-byte-8-bit http://stackoverflow.com/questions/5516044/system-where-1-by... I think in the context of the original answer there's also the problem that char types aren't necessarily 8 bits wide, e.g. wchar etc.
- pdonis 10y agoOk, I should have been a bit more specific: aren't all bytes on machines that Python can realistically run on 8 bits wide?
- lisper 10y agoNo. All modern machines can address 8-bit-wide bytes for historical reasons, but they can also address wider bytes. 32 and 64 bit bytes are common. We call them "words" instead of "bytes", but they're really the same thing: a chunk of data that can be operated on as a single unit by the hardware). And having that capability exposed as a software abstraction can be extremely useful, particularly in applications like cryptography. EDIT: Downvotes? Seriously?
- pdonis 10y ago> a chunk of data that can be operated on as a single unit by the hardware I didn't realize that this concept was referred to by the term "byte". But this clarifies how you are using that term. Given that usage, I'm not sure why 8-bit bytes are an issue at all for a language like Python that is supposed to be hardware-agnostic; the whole point is that programs should not care about how the data is physically represented in the hardware. Python 3 got rid of the distinction between int and long for the same reason. The only reason the "bytes" object exists at all is to represent binary data, for example data that gets read from/written to files, network sockets, and other I/O, and AFAIK bytes in such data are always defined to be 8-bit chunks of data. (If you really insist on having Python representations of hardware data chunks, there's the ctypes module.)