Skip to content

Tag: python

Python: if not val, vs if val is None

I’ve always coded in the style of if not value, however, a few guides have brought to my attention that while this style works, it seems to have 2 potential problems: It’s not completely readable; if value is None is surely more understandable. This can have implications later (and cause subtle bu…

Subclass dict: UserDict, dict or ABC?

What’s the difference between UserDict, dict and ABC and which one is recommended? The docs seem to deprecate UserDict? Also it seems UserDict’s update() would use my setitem method whereas dict doesn’t? Which methods are really essential to override given I want custom setitem and getitem f…

Build the full path filename in Python

I need to pass a file path name to a module. How do I build the file path from a directory name, base filename, and a file format string? The directory may or may not exist at the time of call. For example: I need to create a string ‘/home/me/dev/my_reports/daily_report.pdf’ Concatenating the piec…

Is it reasonable to use None as a dictionary key in Python?

None seems to work as a dictionary key, but I am wondering if that will just lead to trouble later. For example, this works: The actual data I am working with is educational standards. Every standard is associated with a content area. Some standards are also associated with content subareas. I would like to m…

ctypes variable length structures

Ever since I read Dave Beazley’s post on binary I/O handling (http://dabeaz.blogspot.com/2009/08/python-binary-io-handling.html) I’ve wanted to create a Python library for a certain wire protocol. However, I can’t find the best solution for variable length structures. Here’s what I wan…