Is there any difference between UserDict and Dict?

If I want a class to have behavior in a dictionary, why should I inherit from dictor UserDict?

+3
source share
1 answer

You can inherit from dicton any Python version 2.2 or higher, but you will have to override each method you are interested in - for example, your override __getitem__will not be used getunless you also override this, etc. etc.

Mixing UserDict.DictMixingoes back much further and allows only a few methods to be implemented: the other methods provided with the mix-in will collect and use your own overrides. Please note, however, the documents :

Starting with Python 2.6, it is recommended that you use collection.MutableMapping instead of DictMixin.

The new ABCs (abstract base classes) in the module collectionshave the same advantages as the good old ones UserDict.DictMixin, wider application possibilities and more regularity.

+8
source

Source: https://habr.com/ru/post/1728865/


All Articles