ipaddress的IPv4Address.compressed含义

问题描述 投票:0回答:2

当我阅读ipaddress文档时:

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

当我读到IPv4Address.compressed时,我发现compressed没有解释。

谁能告诉我这是什么意思?


source code,只有Return the shorthand version of the IP address as a string.解释,我不太了解shorthand version of the IP address

python ip-address
2个回答
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属性,则在处理混合地址类型时使用它会更麻烦。


2
投票

我发现压缩没有解释

它实际上是documented和爆炸的属性:

压缩

爆炸

点分十进制表示法的字符串表示。前导零从不包含在表示中。

由于IPv4没有为八位字节设置为零的地址定义简写符号,因此这两个属性始终与IPv4地址的str(addr)相同。公开这些属性可以更容易地编写可以处理IPv4和IPv6地址的显示代码。

该属性本身是IPv4和IPv6地址的基类中的defined,如下所示:

@property
def compressed(self):
    """Return the shorthand version of the IP address as a string."""
    return str(self)

对于IPv4Address对象,str(self)将以十进制数字符号返回string,例如: "192.168.0.1"

© www.soinside.com 2019 - 2024. All rights reserved.