6 ms·
Something is wrong if there is no third type: the "natural" string (bytes on Python 2, unicode on Python 3).
by d0mine 7y ago
Something is wrong if there is no third type: the "natural" string (bytes on Python 2, unicode on Python 3).
- war1025 7y agoI assume many of the strings were left untouched. But you still have to audit all of it to know which needs to be used where.
- eesmith 7y agoI believe that's included in the "etc."
- quietbritishjim 7y agoSurely any "natural" string would be better represented as unicode in Python 2? What is an example that wouldn't be?
- war1025 7y agoI believe what they meant was that for many strings it really shouldn't matter if they were bytes or unicode. They would perform their function correctly either way. That's completely true, but you do still have to go through and find the cases where that doesn't work.
- deleted 7y ago[deleted]
- masklinn 7y ago> Surely any "natural" string would be better represented as unicode in Python 2? No because much of the stdlib works in terms of native strings and will choke (or worse silently fuck up) on the other. Yes also in python 2, the stdlib was absolutely not “unicode clean”. So a transitional / polyglot codebase has and needs not 2 but 3 string types: bytes, unicode, and native. And neither “unicode literals” nor “bytes literals” were good things to apply across the board.
- cjbillington 7y agoI've found myself defining `native_str = bytes if PY2 else str` (with `if PY2: str = unicode` at the top of the file, as in all my py2/py3 polyglot code) because there are some things that need bytes on Python 2 and unicode on Python 3 - e.g. the `__file__` attribute of a dynamically created module or other low-level things.
- jnwatson 7y agoThere is. It looks like this: u"Hello World"