3 ms·
You shouldn't be moving things like that around in strings. You should be moving them around as bytes, then decoding them when you need to manipulate them. Your
by Sir_Cmpwn 10y ago
You shouldn't be moving things like that around in strings. You should be moving them around as bytes, then decoding them when you need to manipulate them. Your strategy is probably going to lead to subtly broken behaviors in your program. The problem here is not how Python 3 handles strings, it's how you're handling strings.
>It doesn't help that Python's poor type management tools mean that your first indication you mis-handled a string in Python 3 is the same as Python 2 - unexpected output to a buffer (often b'string' instead of terminal-killing garbage).
I agree, Python's behavior around implicit conversion of things to str is pretty bad. It would be better to throw a TypeError.
- aeturnum 10y agoThis is exactly what I mean. I know I should move binary data around as bytes. I'm not saying I want to (or intend to) move binary data around as strings. I'm saying that, in Python 2, you get a str - which happens to be binary and you don't have to worry about it. Even if it's unicode, it'll still work - it just won't matter. In Python 3, it depends on what the library gives you. Did it give you bytes? Maybe a str? You'll need to check and do the proper conversion to bytes. The behavior that Python 3 forces on you is safer and more correct, but it's behavior that you did not have to go through in Python 2. Sometimes, in Python 3, the library gives you bytes and you want bytes and you're fine, but it's common that you are not.