[Python-Dev] PEP 3144 review.

Stephen J. Turnbull stephen at xemacs.org
Wed Sep 30 14:13:38 CEST 2009


Mark Dickinson writes:

 > >> Please could someone who understands the uses of IPNetwork
 > >> better than I do explain why the following wouldn't be a
 > >> significant problem, if __eq__ and __hash__ were modified to
 > >> disregard the .ip attribute as suggested:

 > >>>>> linus = IPv4Network('172.16.200.1/24')
 > >>>>> snoopy = IPv4Network('172.16.200.3/24')
 > >>>>> fqdn = {linus: 'linus.peanuts.net', snoopy: 'snoopy.peanuts.net'}
 > >>>>> fqdn[linus]  # expecting 'linus.peanuts.net'

 > >> 'snoopy.peanuts.net'

Well, for one thing it would be broken anyway if __eq__ and __hash__
paid attention to the .ip attribute, because they *also* pay attention
to the .prefixlen (and I guess .network, redundantly) attribute.
Presumably the use in real code would not be fqdn[linus], you already
know about linus.  Rather, it would be equivalent to say

>>>>> fqdn[IPv4Network('172.16.200.1/16')]
KeyError

where an IP and network parsed out of some packet were used to
construct the key.  Of course it would work almost all the time, and
only break when, say, old-style class was used to infer the prefix
length, and was wrong.  So, no, I don't think we want to do this.




More information about the Python-Dev mailing list