[Python-Dev] cpython: Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__

Antoine Pitrou solipsis at pitrou.net
Sat Jul 7 15:55:35 CEST 2012


On Sat,  7 Jul 2012 15:08:42 +0200 (CEST)
nick.coghlan <python-checkins at python.org> wrote:
>  
>      def __eq__(self, other):
> -        if not isinstance(other, _BaseNetwork):
> -            raise TypeError('%s and %s are not of the same type' % (
> -                             self, other))
> -        return (self._version == other._version and
> -                self.network_address == other.network_address and
> -                int(self.netmask) == int(other.netmask))
> +        try:
> +            return (self._version == other._version and
> +                    self.network_address == other.network_address and
> +                    int(self.netmask) == int(other.netmask))
> +        except AttributeError:
> +            return NotImplemented

I think the isinstance() test was correct. If you have an object which
duck-types IPNetwork, you probably want its __eq__ to be called, not
yours.

Regards

Antoine.




More information about the Python-Dev mailing list