Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't Python dicts unified?

Tags:

python

After reading this question, I noticed that S. Lott might have liked to use an “ordered defaultdict”, but it doesn't exist. Now, I wonder: Why do we have so many dict classes in Python?

  • dict
  • blist.sorteddict
  • collections.OrderedDict
  • collections.defaultdict
  • weakref.WeakKeyDictionary
  • weakref.WeakValueDictionary
  • others?

Why not have something like this,

dict(initializer=[], sorted=False, ordered=False, default=None, 
     weak_keys=False, weak_values=False)

that unifies everything, and provides every useful combination?

like image 208
Neil G Avatar asked Jul 06 '11 20:07

Neil G


1 Answers

One issue is that making this change would break backward-compatibility, due to this type of constructor usage that exists now:

>>> dict(one=1, two=2)
{'two': 2, 'one': 1}
like image 148
FogleBird Avatar answered Oct 23 '22 07:10

FogleBird