python - Inheriting from defaultddict and OrderedDict -


i have tried inheriting both collections.defaultdict , collections.ordereddict so:

class ordereddefaultdict(defaultdict, ordereddict): pass 

and so:

class ordereddefaultdict(ordereddict, defaultdict): pass 

in order created ordered default dict (a class should remember order in new default items generated) seems throw error stating:

typeerror: multiple bases have instance lay-out conflict 

i have read here it's because they're both implemented in c. upon disabling python's c implementation of ordereddict commenting section in collections module:

try:     _collections import ordereddict except importerror:     # leave pure python version in place.     pass 

i have managed inherit both, not seem work expected. have 2 questions then:

  1. why can't inherit 2 classes written in c? understand if wouldn't able inherit class written in c why can inherit 1 , not 2?
  2. implementing requested class easy subclassing ordereddict , overriding __getitem__() match defaultdict's behavior. is there better way (preferably within standard library) mimick same behavior? or subclassing ordereddict optimal? (subclassing defaultdict possible implementing is easier using ordereddict)

the first question important of two implementing myself subclassing ordereddict shouldn't pose problem.

the functionality of defaultdict can implemented defining __missing__ method:

class defaultordereddict(ordereddict):     def __init__(self, default_factory=none, **kwargs):         ordereddict.__init__(self, **kwargs)         self.default_factory = default_factory      def __missing__(self, key):         result = self[key] = self.default_factory()         return result 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -