IPv4Address.compressed meaning for ipaddress

时间:2017-12-18 07:48:33

标签: python ip-address

When I read the ipaddress docs:

https://docs.python.org/3/library/ipaddress.html#ipaddress.IPv4Address.compressed

When I read the children, I find there is no explain for the IPv4Address.compressed.

Who can tell me what is it mean ?


From the source code, there is only compressed explain, I am not well understand the Return the shorthand version of the IP address as a string..

2 个答案:

答案 0 :(得分:3)

compressedexploded是由基类ipaddress._IPAddressBase定义的属性,因此每个ip地址实例都有它们。对于IPv4,两者之间没有区别,因为历史上不需要更短的表示:

>>> i4 = ipaddress.IPv4Address("127.0.0.1")
>>> i4.exploded
'127.0.0.1'
>>> i4.compressed
'127.0.0.1'

不同之处在于ipv6地址:

>>> i6 = ipaddress.IPv6Address("::1")
>>> i6.exploded
'0000:0000:0000:0000:0000:0000:0000:0001'
>>> i6.compressed
'::1'

这里省略0组对可用性有很大帮助。

由于所有地址都具有这两个属性,因此您无需关心地址对象的类型。如果只有IPv6Address个对象具有exploded属性,则在处理混合地址类型时使用它会更麻烦。

答案 1 :(得分:2)

I find there is no explain for the compressed

It's actually documented together with the exploded property:

compressed

exploded

The string representation in dotted decimal notation. Leading zeroes are never included in the representation.

As IPv4 does not define a shorthand notation for addresses with octets set to zero, these two attributes are always the same as A = [18985.0, 20491.0, 18554.0, 14241.0, 13390.0, 14965.0] A = [0] + [1 if A[i] > A[i-1] else 0 for i in range(1, len(A))] for IPv4 addresses. Exposing these attributes makes it easier to write display code that can handle both IPv4 and IPv6 addresses.

The property itself is defined in the base class for both IPv4 and IPv6 addresses as follows:

str(addr)

For an @property def compressed(self): """Return the shorthand version of the IP address as a string.""" return str(self) object IPv4Address would return a string in decimal-dot notation, e.g. str(self).