Eric Osborne <> added the comment:

It is often useful to have non-decimal representations of IP addresses.
Hex shows up a lot in sniffer traces, which is why I wanted to provide
__index__, but that's not going to happen.  I use binary a lot when
teaching subnet masking and address summarization - if you line up bit
patterns it's much easier to show how things lay out.  It's easy enough to
use bin(int(addr)) but that doesn't zero-pad the string it returns.  I find
myself doing something like

In [23]: a
Out[23]: IPv4Address('')

In [24]: x = bin(int(a))[2:]

In [25]: full_x = '0b' + ('0' * (32-len(x)) + x)

In [26]: full_x
Out[26]: '0b00000001000000100000001100000100'

Although, as Eric Smith has pointed out, there's a one liner way to do
this. But if an IP address can represent itself as an integer (IMO the
least useful form) it should have at least a binary representation, and
lack of a seperate __bin__ means this is as close as I could get.


On Mon, Feb 12, 2018 at 7:39 AM Christian Heimes <>

> Christian Heimes <> added the comment:
> I agree with Serhiy and Eric. It's a needless complication of the module.
> What's the actual use case of printing a human readable bit representation
> of an IP address?
> ----------
> nosy: +christian.heimes
> _______________________________________
> Python tracker <>
> <>
> _______________________________________


Python tracker <>
Python-bugs-list mailing list

Reply via email to