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

Nick Coghlan ncoghlan at gmail.com
Sat Jul 7 16:59:51 CEST 2012


On Sat, Jul 7, 2012 at 11:55 PM, Antoine Pitrou <solipsis at pitrou.net> 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.com   |   Brisbane, Australia


More information about the Python-Dev mailing list