Extending Built-ins
Understand how to extend Python's built-in collections by customizing dictionary behavior. Explore subclassing to prevent duplicate keys, override methods like __init__, __setitem__, and update, and apply type hints for reliable code. This lesson equips you to create robust, specialized dictionary subclasses compatible with Python's standard library.
We'll cover the following...
Mutable and immutable objects
Python has two collections of built-ins that we might want to extend. We can broadly classify these into the following:
-
Immutable objects, including numbers, strings, bytes, and tuples. These will often have extended operators defined. In the operator overloading section, we looked at how to provide arithmetic operations for objects of the
Diceclass. -
Mutable collections, including sets, lists, and dictionaries. When we look at the definitions in
collections.abc, these are sized, iterable containers, three distinct aspects that we might want to focus on.
There are other built-in types, but ...