sorteddict PEP proposal [started off as orderedict]

Paul Hankin paul.hankin at gmail.com
Tue Sep 25 23:33:23 CEST 2007


On Sep 25, 9:55 pm, Mark Summerfield <m.n.summerfi... at googlemail.com>
wrote:
> ...
> class sorteddict(dict):
>
> ...
>         if self.__keys is None:
>             self.__keys = sorted(dict.keys(self), cmp=self.__cmp,
>                                  key=self.__key,
> reverse=self.__reverse)

You'd be better defining __keys like this:

def __getkeys(self):
  if self.__keycache is None:
     self.__keycache = dict.keys(self)
     self.__keycache.sort(cmp=...)
  return self.__keycache

__keys = property(__getkeys)

and where you have 'self.__keys = None', either replace with
'self.__keycache = None' or more cleanly with a call to:

def __invalidate_key_cache(self):
    self.__keycache = None

to improve the code quality.

--
Paul Hankin




More information about the Python-list mailing list