Exposing Python 3.6's Private Dict Version

I just got home from my sixth PyCon, and it was wonderful as usual. If you weren’t able to attend—or even if you were—you’ll find a wealth of entertaining and informative talks on the PyCon 2017 YouTube channel.

Two of my favorites this year were a complementary pair of talks on Python dictionaries by two PyCon regulars: Raymond Hettinger’s Modern Python Dictionaries A confluence of a dozen great ideas and Brandon Rhodes’ The Dictionary Even Mightier (a followup of his PyCon 2010 talk, The Mighty Dictionary)

Raymond’s is a fascinating dive into the guts of the CPython dict implementation, while Brandon’s focuses more on recent improvements in the dict’s user-facing API. One piece both mention is the addition in Python 3.6 of a private dictionary version to aid CPython optimization efforts. In Brandon’s words:

“PEP509 added a private version number… every dictionary has a version number, and elsewhere in memory a master version counter. And when you go and change a dictionary the master counter is incremented from a million to a million and one, and that value a million and one is written into the version number of that dictionary. So what this means is that you can come back later and know if it’s been modified, without reading maybe its hundreds of keys and values: you just look and see if the version has increased since the last time you were there.”

He later went on to say,

“[The version number] is internal; I haven’t seen an interface for users to get to it…”

which, of course, I saw as an implicit challenge. So let’s expose it!