Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Dev

Re: cpython: Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__

 

 

Python dev RSS feed   Index | Next | Previous | View Threaded


solipsis at pitrou

Jul 7, 2012, 6:55 AM

Post #1 of 3 (141 views)
Permalink
Re: cpython: Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__

On Sat, 7 Jul 2012 15:08:42 +0200 (CEST)
nick.coghlan <python-checkins [at] python> 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.


_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


ncoghlan at gmail

Jul 7, 2012, 7:59 AM

Post #2 of 3 (136 views)
Permalink
Re: cpython: Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__ [In reply to]

On Sat, Jul 7, 2012 at 11:55 PM, Antoine Pitrou <solipsis [at] pitrou> wrote:
> 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.

This change was just to bring IPNetwork in line with the other types
in the module and to stop it throwing TypeError itself, which meant
the RHS was never even getting a chance to affect the result.

Ducktyping and operator overloading has always been a tricky area
though. If you use isinstance() checks, then the other side has to
know how to reimplement your equality check, or temporarily create a
real instance to do the comparison. If you use ducktyping internally,
then the other side *has* to use inheritance if they want to
completely control the result, but also have the option to just expose
the appropriate attributes in order to interoperate with your class.

The standard library tends to be a mixture of both based on how
integral the author feels the ordering and comparison behaviour is to
the classes involved. In this case, I currently think internal
ducktyping is the right answer, but I'm open to being persuaded
otherwise.

Cheers,
Nick.

--
Nick Coghlan | ncoghlan [at] gmail | Brisbane, Australia
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com


ncoghlan at gmail

Jul 7, 2012, 8:18 AM

Post #3 of 3 (136 views)
Permalink
Re: cpython: Issue 14814: Correctly return NotImplemented from ipaddress._BaseNetwork.__eq__ [In reply to]

On Sun, Jul 8, 2012 at 12:59 AM, Nick Coghlan <ncoghlan [at] gmail> wrote:
> On Sat, Jul 7, 2012 at 11:55 PM, Antoine Pitrou <solipsis [at] pitrou> wrote:
>> 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.
>
> This change was just to bring IPNetwork in line with the other types
> in the module and to stop it throwing TypeError itself, which meant
> the RHS was never even getting a chance to affect the result.

Hmm, I just noticed the __lt__ implementations still throw TypeError
directly (at least in the IPNetwork case). Looks like some more
cleanups are still needed in this area :P

Cheers,
Nick.

--
Nick Coghlan | ncoghlan [at] gmail | Brisbane, Australia
_______________________________________________
Python-Dev mailing list
Python-Dev [at] python
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: http://mail.python.org/mailman/options/python-dev/list-python-dev%40lists.gossamer-threads.com

Python dev RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.