django - How can I write my own OrderedDict class in Python? -
basically want understand how thing works under hood. tried create list of tuples [(), (), ...] , each tuple have 2 values, qst wuld key , second value. not actual dictionary , there performance issue (i mean read/write/delete operations.
so how should write class myordereddict (probably) extending default dict class.
any hint or resource appreciated.
ps: similar functional have in django. sorteddict
from django.utils.datastructures import sorteddict
are both same or follow same approach in implementation?
help on class sorteddict in module django.utils.datastructures: class sorteddict(__builtin__.dict) | dictionary keeps keys in order in they're inserted. | | method resolution order: | sorteddict | __builtin__.dict | __builtin__.object | | methods defined here:
the class manage dictionary , list. behind scenes, every standard dict operation use both:
dvalues = {"foo":"x","bar":"y","baz":"z"} lorder = ["bar","baz","foo"] to value key, standard dictionary:
return dvalues[key] to value order m entered:
return dvalues[lorder[m]] add key,value:
dvalues[key] = value lorder.append(key) etc.
Comments
Post a Comment