Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 function?

With ABCs I'd have to implement absolutely all methods since it provides no default implementation?

I want to make a dict that does two things:

  • intern() all keys and values
  • store some of the values in an SQLite database

So which of UserDict, dict and ABC would best allow me to do this?

like image 717
Gerenuk Avatar asked Aug 22 '11 13:08

Gerenuk


People also ask

What is UserDict in Python?

The UserDict module contains a dictionary class that can be subclassed (it's actually a Python wrapper for the built-in dictionary type). Example 2-15 shows an enhanced dictionary class, which allows dictionaries to be “added” to each other and initialized using the keyword argument syntax.

Is dictionary a class in Python?

Python Dictionary is a set of key-value pairs. A dictionary is an object of class dict. It's an unordered collection means that while iterating the order of retrieval is not guaranteed. The dictionary keys and values can be of any type.

How do you write a dictionary in python?

Creating Python Dictionary Creating a dictionary is as simple as placing items inside curly braces {} separated by commas. An item has a key and a corresponding value that is expressed as a pair (key: value).


2 Answers

If you want a custom collection that actually holds the data, subclass dict. This is especially useful if you want to extend the interface (e.g., add methods).

None of the built-in methods will call your custom __getitem__ / __setitem__, though. If you need total control over these, create a custom class that implements the collections.MutableMapping abstract base class instead.

The ABC does not provide a means to store the actual data, only an interface with default implementations for some methods. These default implementations will, however, call your custom __getitem__ and __setitem__. You will have to use an internal dict to hold the data, and implement all abstract methods: __len__, __iter__, __getitem__, __setitem__, and __delitem__.

The class UserDict from the collections module (in Python 2, the module is called UserDict as well) is a wrapper around an internal dict, implementing the MutableMapping ABC. If you want to customize the behavior of a dict, this implementation could be a starting point.

In summary:

  • MutableMapping defines the interface. Subclass this to create something that acts like a dict. It's totally up to you if and how you store the data.
  • UserDict is an implementation of MutableMapping using an internal "real" dict as storage. If you want a dict-like storage collection but override some methods exposed by dict, this might be a good starting point for you. But make sure to read the code to know how the basic methods are implemented, so that you are consistent when overriding a method.
  • dict is "the real thing". Subclass this if you want to extend the interface. Overriding methods to do custom things might be dangerous, as there are usually multiple ways of accessing the data, and you could end up with an inconsistent API.
like image 157
Ferdinand Beyer Avatar answered Oct 14 '22 16:10

Ferdinand Beyer


Based on Secrets Recipes of the Python Ninja book

The only special thing the UserDict has beyond the normal dictionary operations is a single attribute:

data: A real dictionary to hold the contents of the UserDict class

To get to the items in the dictionary, you have to either iterate over them or call items(). While the UserDict instance supports the same methods, the view returned by items() is noticeably different:

  >>> from collections import UserDict   >>> a = UserDict(a=1)   >>> d = dict(d=3)  # regular dictionary for comparison    >>> for k in d:   ...     print(k, d[k])   ...    d 3   >>> d.items()   dict_items([('d', 3)])   >>> for k in a:   ...     print(k, a[k])   ...    a 1   >>> a.items()   ItemsView({'a': 1}) 

Notice that the dictionary object returns a tuple of key/values. The UserDict returns an actual dictionary object. Depending on what you are doing, this difference can be important, as is the ability to use the data attribute to access the dictionary.

like image 22
Vlad Bezden Avatar answered Oct 14 '22 14:10

Vlad Bezden