dictionary interface
Robert Kern
robert.kern at gmail.com
Tue Oct 4 09:34:36 EDT 2005
Antoon Pardon wrote:
> I'm writing a Tree class, which should behave a lot like a dictionary.
>
> In order to test this, I took the unittest from the source distribution
> for dictionaries and used it to test against my Tree class.
>
> Things are working out rather well, but I stumbled on a problem.
>
> this unittest tries to test for '==' and '<' operators. However I
> couldn't find anything in the documentation that defined how
> dictionaries should behave with respect to these operators.
>
> For the moment the best I can come up with is something like
> the following:
>
> class Tree:
>
> def __lt__(self, term):
> return set(self.iteritems()) < set(term.iteritems())
>
> def __eq__(self, term):
> return set(self.iteritems()) == set(term.iteritems())
>
> Would this be a correct definition of the desired behaviour?
No.
In [1]: {1:2} < {3:4}
Out[1]: True
In [2]: set({1:2}.iteritems()) < set({3:4}.iteritems())
Out[2]: False
> Anyone a reference?
The function dict_compare in dictobject.c .
--
Robert Kern
rkern at ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the Python-list
mailing list