[Python-Dev] PEP 3144 review.
Scott Dial
scott+python-dev at scottdial.com
Tue Sep 15 21:13:27 CEST 2009
Peter Moody wrote:
> but that's what's being suggested here for networks.
>
>>>> ipaddr.IPv4Network('1.1.1.1/24')[0][1][2][3]...
This example here solidifies my support of RDM's suggestion of there
being 3 types:
IPv4Address
IPv4AddressWithNetwork (or as he called it: IPv4AddressWithMask)
IPv4Network
The primary difference between IPv4Network and IPv4AddressWithNetwork
would be that IPv4AddressWithNetwork would not support
iteration/indexing. The example above makes no sense logically and my
original suggestion unknowingly opened that can of worms. If indexing a
IPv4Network returned IPv4AddressWithNetwork, then that would remove that
oddity.
This would also solve the weirdness that Stephen brought up in another
branch of this discussion:
Stephen J. Turnbull wrote:
> Scott Dial writes:
> > ipaddr.IPv4Network('1.1.1.0/24')[0] ==
> > ipaddr.IPv4Network('1.1.1.0/24')
>
> So foo returns True?
>
> def foo():
> a = ipaddr.IPv4Network('1.1.1.0/24')
> return a[0] == a
>
> That seems ... weird. Maybe you committed a typo?
The root of the weirdness is that my proposition would appear to make
IPv4Network play double-duty. However, it already does! That you can
instantiate a IPv4Network object with a non-zero host is a clue, and
that it is apparently the only way to input a host with a mask is the
second clue.
If I have an IP (10.2.3.4) and I know the netmask (say, 255.255.0.0),
then how do I get the network that it is on? As it stands, the only way
I see to do this is to do:
>>> net = ipaddr.IPv4Network("10.2.3.4/255.255.0.0")
Which is strange, because I didn't input a *network*, I inputted an
*address* on a network. Moreover, it's strange because:
>>> net[0] == net.ip
False
If I needed that identity to hold true, then I have to do:
>>> net = ipaddr.IPv4Network("%s/%s" % (net[0], net.netmask)
Although it is unclear why a "network" has an "ip" attribute. As far as
I am concerned, IPv4Network objects should *only* have a net.network.
Hopefully at this point, I have made the case that IPv4Network already
is playing double-duty as a IPv4Network and IPv4AddressWithNetwork. And
that the API would be well-served by splitting that role and that it
would be considerably less confusing.
--
Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu
More information about the Python-Dev
mailing list