j
: Next unread message k
: Previous unread message j a
: Jump to all threads
j l
: Jump to MailingList overview
On 15 February 2018 at 10:18, Steven D'Aprano steve@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 | ncoghlan@gmail.com | Brisbane, Australia