python - Which is the appropriate way to test for a dictionary type? -
Why there are so many different ways to test for a dictionary? And if an object is a dictionary, what would be the most modern way to test it? [10]: isinstance (adict, types.DictType) out [10]: Really [11]: isinstance (adict, types.DictionaryType)
adict = {'a': 1} Out [11]: Really [12]: Isinstance (adict, dict) Out [12]: Really [13]: Isinstance (adict, collection mapping) Out [13]: Really [14]: isinstance ( Adict, collections.MutableMapping) [14]: True
types DictType and types.DictionaryType have been deprecated (well, removed in Python 3) dict . Archive. Mapping and collections. Matable Mapping are abstract base classes (ABC) so that they work together Mapping that do not subclass from dict generally makes them a better choice , Although very stringent checks are helpful at times. So basically, check the opposite
-
None of them,
-
< Code> Archive. Mapping If you do not need a mutation
-
-
dict / Em> a dict type (it should be rare) -
types.DictType Or types.DictionaryType if you want to support actually old version
Comments
Post a Comment