Add __len__ to ipaddress._BaseNetwork

Currently, ipaddress._BaseNetwork (and by extension, ipaddress.IPv4Network and ipaddress.IPv6Network) does not have a __len__ method, it only has num_addresses. This makes the following code not work:
The workaround is a bit ugly:
(network := ipaddress.ip_network('6.0.0.0/8'))[random.randrange(network.num_addresses)] IPv4Address('6.60.184.142')
With a custom subclass, all works well:

BEst thing you can do is create an issue on bugs.python.org and attach a pull request that makes _BaseNetwork a subclass of Sequence. Then maybe the advantages will be clear -- or maybe in the process of writing the code you realize that the idea is not so good after all. (I'm not a user of this class so I don't have an opinion either way.) On Tue, Oct 26, 2021 at 6:41 AM <nyuszika7h@gmail.com> wrote:
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>

BEst thing you can do is create an issue on bugs.python.org and attach a pull request that makes _BaseNetwork a subclass of Sequence. Then maybe the advantages will be clear -- or maybe in the process of writing the code you realize that the idea is not so good after all. (I'm not a user of this class so I don't have an opinion either way.) On Tue, Oct 26, 2021 at 6:41 AM <nyuszika7h@gmail.com> wrote:
-- --Guido van Rossum (python.org/~guido) *Pronouns: he/him **(why is my pronoun here?)* <http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-c...>
participants (2)
-
Guido van Rossum
-
nyuszika7h@gmail.com