How to check something is in a list with rich-comparison objects?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue Sep 15 14:39:16 EDT 2009
En Tue, 15 Sep 2009 11:18:33 -0300, Jason <jason.heeris at gmail.com>
escribió:
> Comparing a string to the enumerations in pysvn gives me an attribute
> error, because they've overloaded the rich compare methods:
>
>>>> import pysvn
>>>> "string" in [pysvn.wc_notify_action.status_completed, "string"]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> AttributeError: expecting wc_notify_action object for rich compare
>
> Is there a simple way around this?
Looks like a bug in pysvn. Some class (whatever
pysvn.wc_notify_action.status_completed is) is not well written. When
compared against something unknown, it should return NotImplemented
instead of raising AttributeError.
py> class BadBoy(object):
... foo = 1
... #
... def __eq__(self, other):
... if not isinstance(other, BadBoy):
... raise TypeError, "expecting BadBoy object for rich compare"
... return self.foo==other.foo
...
py> "hello" in [BadBoy(), "hello"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in __eq__
TypeError: expecting BadBoy object for rich compare
py>
py> class GoodBoy(object):
... foo = 1
... #
... def __eq__(self, other):
... if not isinstance(other, GoodBoy):
... return NotImplemented
... return self.foo==other.foo
...
py> "hello" in [GoodBoy(), "hello"]
True
--
Gabriel Genellina
More information about the Python-list
mailing list