[Biopython-dev] Equality in Bio.Restriction.RestrictionType

Peter biopython at maubp.freeserve.co.uk
Wed Jul 28 16:08:46 UTC 2010


2010/7/8 Peter <biopython at maubp.freeserve.co.uk>:
> Hi Frédéric et al,
>
> One of the things in Python 3 is that overriding equality (done with __eq__
> only since __cmp__ has gone) requires you also override __hash__. One
> remaining example of this which triggers a deprecation warning within our
> test suite when running with the -3 switch in in Bio.Restriction.
>
> I therefore had a look at how __eq__ and __ne__ are defined in the
> RestrictionType class - and strangely they do NOT seem to be inverses.
>
>    def __eq__(cls, other):
>        """RE == other -> bool
>
>        True if RE and other are the same enzyme."""
>        return other is cls
>
>    def __ne__(cls, other):
>        """RE != other -> bool.
>        isoschizomer strict, same recognition site, same restriction -> False
>        all the other-> True"""
>        if not isinstance(other, RestrictionType):
>            return True
>        elif cls.charac == other.charac:
>            return False
>        else:
>            return True
>
> Frédéric - could you clarify the intent here?

Hi Frédéric,

As implemented, __eq__ just seems to check for object identity,
effectively id(a) == id(b), so to make the unit test pass on Python 3
all I needed to do here was define __hash__ explicitly to return id(self),
which is the default behaviour under Python 2.

I'm still puzzled about the reasoning behind the comparisons.
Clearly you had something special in mind with these definitions as
shown by the test_Restriction.py unit tests under test_comparisons,

        assert Acc65I == Acc65I
        assert not(Acc65I == Asp718I)
        assert not(Acc65I != Asp718I)

Note that Acc65I.site == Asp718I.site == 'GGTACC', and also
Acc65I.charac == Asp718I.charac == (1, -1, None, None, 'GGTACC')

It looks to me like Acc65I and Asp718I differ only in name, and you
wanted both Acc65I == Asp718 and Acc65I != Asp718I to return
False. i.e. They are neither equal nor non-equal, but somewhere
in between?

Regards,

Peter




More information about the Biopython-dev mailing list