Why are there many different ways to check a dictionary? And what would be the most modern way to check if an object is a dictionary?
adict = {'a': 1} In [10]: isinstance(adict, types.DictType) Out[10]: True In [11]: isinstance(adict, types.DictionaryType) Out[11]: True In [12]: isinstance(adict, dict) Out[12]: True In [13]: isinstance(adict, collections.Mapping) Out[13]: True In [14]: isinstance(adict, collections.MutableMapping) Out[14]: True
types.DictTypeand types.DictionaryTypeoutdated (well, removed in Python 3) aliases dict.
types.DictType
types.DictionaryType
dict
collections.Mappingand collections.MutableMappingare abstract base classes (ABC), so they work with mappings that are not a subclass of dict. This usually makes them a better choice, although occasionally a more rigorous version of typecheck may be required.
collections.Mapping
collections.MutableMapping
So basically check that
None of them, if possible (duck type)
collections.Mappingif you do not need a mutation
collections.MutableMapping,
dict, , dict ( )
types.DictType types.DictionaryType,
-, types.DictType, types.DictionaryType dict ( , dict).
True , dict. , , -, .. , dict. : Mapping ( ), MutableMapping , .
True
Mapping
MutableMapping
Source: https://habr.com/ru/post/1538154/More articles:Time Tracking in TFS Using VS2013 - tfsoptional python arguments without a dash, but with extra parameters? - pythonVimdiff message suppression - scriptingHow can I get / replace an iframe in a Chrome app? - javascriptEmbedding a custom class in a subclass inheritance tree in Objective-C - ioshttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1538155/how-would-you-explain-javascript-typed-arrays-to-someone-with-no-programming-experience-outside-of-javascript&usg=ALkJrhiwlxnHt4SnAmcSfjexIpeObdSgJQHow to link tuple list using Spring JDBCTemplate? - springHow to insert Python code block in Notepad ++? - pythonWhy reCaptcha Stealing Focus on page loading - javascriptIs there a way to change the font and location of SpriteKit debugging data? - sprite-kitAll Articles