|
|
Subscribe / Log in / New account

Python 2.8?

Python 2.8?

Posted Jan 14, 2017 5:45 UTC (Sat) by lsl (subscriber, #86508)
In reply to: Python 2.8? by ddevault
Parent article: Python 2.8?

> If you ever try to do string manipulation on a bytes, you're Doing It Wrong. Bytes _may_ be an encoded string, but to use it you of course have to decode it.

You can redefine terms all you want but there are operations I can reasonably do on bytes that the literature calls "string manipulation". I'm talking about things like splitting on 0x0A ('\n') or 0x2F ('/') bytes. Those are reasonable things to do if whatever you're working on defines them as a reasonable thing to do. I don't have to somehow "decode" the byte string before I can manipulate it. In fact, I cannot possibly decode it as I have no idea (nor a desire to know) what any of these bytes are supposed to mean. The only thing I need to know is that I can legitimately split them upon encountering a '/' byte.

Super simple stuff, until you bring Python 3 into the mix with its desire to enforce specific encodings where none were agreed upon.


to post comments

split() works on bytestrings now

Posted Jan 14, 2017 10:54 UTC (Sat) by rschroev (subscriber, #4164) [Link]

Nowadays split() does work on bytestrings. In Python 3.4:

>>> b'abc/def/ghi'.split(b'/')
[b'abc', b'def', b'ghi']
>>> b'abc def ghi'.split()
[b'abc', b'def', b'ghi']

I think this didn't work in 3.0; I don't know when that changed. IIRC at the same time other string manipulations were implemented fro bytestrings.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds