Python basics: Bytecode

I want to delve into some of the internals of Python, and in order to demonstrate what’s going on I’m going to be taking apart some bytecode. To avoid repeating myself, I’ll get some of the basics out of the way here. Think about the process of executing a piece of Python code. The code […]

Dictionaries preserve ordering

I came across something unexpected when preparing my post about the basics of dictionaries in Python. For as long as I’ve been using Python, it’s been a fundamental truth that dictionaries don’t have a reliable ordering. It’s mentioned all over StackOverflow. The standard library even has an OrderedDict container for cases where you want your […]

Python basics: dict lookup

I had a bunch of posts I wanted to write that would refer to dict lookup in various ways, and rather than repeat myself in those I thought it would be useful to write a single post that establishes some of the basics. You may already know this stuff, in which case please ignore it. […]

Length hints in Python

I was grubbing about trying to understand some library code the other day (I forget which one) and I came across a double-underscore method I’d never seen before: def __length_hint__(self): return … # Some internals here. Since this is a double-underscore method, it can’t be (or shouldn’t be) something the library invented. It must have […]