The only problem with Python 3's str is that you don't grok it January 13, 2017 on Drew DeVault's blog

I’ve found myself explaining Python 3’s str to people online more and more often lately. There’s this ridiculous claim about that Python 3’s string handling is broken or somehow worse than Python 2, and today I intend to put that myth to rest. Python 2 strings are broken, and Python 3 strings are sane. The only problem is that you don’t grok strings.

The basic problem many people seem to have with Python 3’s strings arises when they write code that treats bytes like a string, because that’s how it was in Python 2. Let me make this as clear as possible:

a bytes is not a string

I want you to read that, over and over again, until it sinks in. A string is basically an array of characters (characters being Unicode codepoints), whereas bytes is an array of bytes, aka octets, aka unsigned 8 bit integers. That’s right - bytes is an array of unsigned 8 bit integers, or as the name would imply, bytes. If you ever do string operations against bytes, you are Doing It Wrong because bytes are not strings.

a bytes is not a string

It’s entirely possible that your bytes contains an encoded representation of a string. That encoding could be ASCII, UTF-8, UTF-32, etc. These encodings are means of representing strings as bytes, aka unsigned 8 bit integers. In order to treat it like a string, you first must decode it. Luckily Python 3 makes this painless: bytes.decode(). This defaults to UTF-8, but you can specify any encoding you want: bytes.decode('latin-1'). If you want bytes again, use str.encode(), which again defaults to UTF-8 but accepts any encoding. If you have a bytes that contains an encoded string, your first order of business is decoding it.

a bytes is not a string

Let’s look at some examples of why this matters in practice:

Python 3.6.0 (default, Dec 24 2016, 08:03:08) 
[GCC 6.2.1 20160830] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 'おはようございます'
>>> 'おはようございます'[::-1]
>>> 'おはようございます'[0]
>>> 'おはようございます'[1]

Or in Python 2:

Python 2.7.13 (default, Dec 21 2016, 07:16:46) 
[GCC 6.2.1 20160830] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 'おはようございます'
>>> 'おはようございます'[::-1]
>>> print('おはようございます'[::-1])
>>> 'おはようございます'[0]
>>> 'おはようございます'[1]

For anything other than ASCII, Python 2 “strings” are broken. Python 3’s string handling is superb. The problem with it has only ever been that you don’t actually know how strings work. Instead of starting ignorant flamewars about it, learn how it works.

Actual examples people have given me

“Python 3 can’t handle bytes as file names”

Yes it can. Just stop treating them like strings:

>>>open(b'test-\xd8\x01.txt', 'w').close()

Note the use of bytes as the file name, not str. \xd8\x01 is unrepresentable as UTF-8.

>>> [open(f, 'r').close() for f in os.listdir(b'.')]

Note the use of bytes as the path to os.listdir (the documentation says that if you want bytes back as file names, pass bytes as the path. The docs are helpful like that). Also note the lack of crashes or broken behavior.

“Python 3’s csv module writes b’Hello',b’World' into CSV files”

CSV files are “comma seperated values”. Is each value an array of unsigned 8 bit integers? No, of course not. They’re strings. So why would you pass an array of unsigned 8 bit integers to it?

“Python 3 doesn’t support writing files as latin-1”

Sure it does.

with open('some latin-1 file', 'rb') as f:
  text ='latin-1')
with open('some utf8 file', 'wb') as f:
a bytes is not a string
a bytes is not a string
a bytes is not a string

Python 2’s shitty design has broken your mindset. Unlearn it.

Python 2 is dead, long live Python 3

Listen. It’s time you moved to Python 3. You’re missing out on a lot of really great improvements to the language and are stuck with a lot of problems. Python 2 is really being EoL’d, and closing your eyes and covering your ears singing “la la la” doesn’t change that. The transition is really not that difficult or time consuming, and well worth it. Some people say only new projects should be written in Python 3. I say that’s bollocks - all projects should be written in Python 3 and you need to migrate, now.

Python 3 is better. Much, much better. For every legitimate criticism of Python 3 I’ve seen, I’ve seen 10 that are bullshit. Come join us in the wonderful world of sane string handling, type decorations, async/await, and more awesome features. Every library supports it now. Let go of your biases and evaluate the language honestly.

Articles from blogs I read Generated by openring

The Bond villain compliance strategy

Jurisdictional gamesmanship is a common strategy for crypto businesses. Here is how it worked out for Binance and its CEO. Spoiler: poorly.

via Bits about Money November 24, 2023

Megapixels 2.0: DNG exporting

It seems overkill to make a whole seperate library dedicated to replacing 177 lines of code in Megapixels that touches libtiff, but this small section of code causes significant issues for distribution packaging and compatability with external photo editi…

via BrixIT Blog November 18, 2023

Status update, November 2023

Hi! This month I’ve started a new PotM called pyonji. It’s an easy-to-use replacement for the venerable git-send-email command. The goal is to make it less painful for a new contributor not familiar with the e-mail based patch submission to submit patches. Use…

via emersion November 16, 2023