On 15 February 2018 at 10:18, Steven D'Aprano <st...@pearwood.info> wrote: > This idea is inspired by Eric Osborne's post "Extending __format__ > method in ipaddress", but I wanted to avoid derailing that thread. > > I notice what seems to be an inconsistency in the ipaddress objects: > > py> v4 = ipaddress.IPv4Address('1.2.3.4') > py> bin(v4) > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: 'IPv4Address' object cannot be interpreted as an integer
That error message should probably either have an "implicitly" in it, or else use the word "handled" rather than "interpreted". There are tests that ensure IP addresses don't implement __index__, and the pragmatic reason for that is the downside you mentioned: to ensure they can't be used as indices, slice endpoints, or range endpoints. While IP addresses can be converted to an integer, they are *not* integers in any mathematical sense, and it doesn't make sense to treat them that way. A useful heuristic for answering the question "Should this type implement __index__?" is "Does this type conform to the numbers.Integral ABC?" (IP addresses definitely don't, as there's no concept of addition, subtraction, multiplication, division, etc - they're discrete entities with a numeric representation, not numbers) Cheers, Nick. -- Nick Coghlan | ncogh...@gmail.com | Brisbane, Australia _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/