Python Tricks: A Buffet of Awesome Python Features

Dan Bader

Last read April 27, 2021

View on Amazon

Highlights

7 highlights.

Techniques like that allow you to communicate clearly about parts of your class architecture so that new development work is naturally guided to happen within these boundaries. Of course, it would be easy enough to defy these restrictions. But in practice, they often help avoid accidental modifications that go against the original design.

Page: 152

using static methods and class methods are ways to communicate developer intent while enforcing that intent enough to avoid most “slip of the mind” mistakes and bugs that would break the design.

Page: 152

Python dictionaries are based on a well-tested and finely tuned hash table implementation that provides the performance characteristics you’d expect: O(1) time complexity for lookup, insert, update, and delete operations in the average case.

Page: 160

You need to store arbitrary objects, potentially with mixed data types? Use a list or a tuple, depending on whether you want an immutable data structure or not.

Page: 173

You have numeric (integer or floating point) data and tight packing and performance is important? Try out array.array and see if it does everything you need. Also, consider going beyond the standard library and try out packages like NumPy or Pandas.

Page: 173

You have textual data represented as Unicode characters? Use Python’s built-in str. If you need a “mutable string,” use a list of characters.

Page: 174

You want to store a contiguous block of bytes? Use the immutable bytes type, or bytearray if you need a mutable data structure.

Page: 174