R
Riccardo Murri
Hello,
I have some code that stops when trying to find a graph in a list of
similar graphs::
(Pydb) list
110 try:
111 canonical = self.base[self.base.index(graph)]
112 except ValueError:
113 raise ValueError, \
114 "Cannot find canonical representative for graph `%s`." \
115 -> % (repr(graph),)
116
....
The list `self.base` contains "canonical" forms of the graphs and the
`graph` object must compare equal to some item of the list, which
indeed it does::
(Pydb) p graph == self.base[27]
True
(Pydb) p graph in self.base
True
However, I cannot directly get the index of the canonical graph (the
number "27" above was found by manual inspection)::
(Pydb) self.base.index(graph)
*** ValueError: list.index(x): x not in list
All graphs are instances of a `Graph` new-style class that implements
comparison operators `__eq__` and `__ne__`, but no other rich-compare
stuff.
I'm using Python 2.5::
Python 2.5 (release25-maint, Dec 9 2006, 16:17:58)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-20)] on linux2
So my question is: what are the implementation differences between `x
in list` and `list.index(x)` and why can one report that an item is in
the list while the other cannot find its index? Should I add
something to the `Graph` class so that `index` works?
Thanks for any hint!
I have some code that stops when trying to find a graph in a list of
similar graphs::
(Pydb) list
110 try:
111 canonical = self.base[self.base.index(graph)]
112 except ValueError:
113 raise ValueError, \
114 "Cannot find canonical representative for graph `%s`." \
115 -> % (repr(graph),)
116
....
The list `self.base` contains "canonical" forms of the graphs and the
`graph` object must compare equal to some item of the list, which
indeed it does::
(Pydb) p graph == self.base[27]
True
(Pydb) p graph in self.base
True
However, I cannot directly get the index of the canonical graph (the
number "27" above was found by manual inspection)::
(Pydb) self.base.index(graph)
*** ValueError: list.index(x): x not in list
All graphs are instances of a `Graph` new-style class that implements
comparison operators `__eq__` and `__ne__`, but no other rich-compare
stuff.
I'm using Python 2.5::
Python 2.5 (release25-maint, Dec 9 2006, 16:17:58)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-20)] on linux2
So my question is: what are the implementation differences between `x
in list` and `list.index(x)` and why can one report that an item is in
the list while the other cannot find its index? Should I add
something to the `Graph` class so that `index` works?
Thanks for any hint!