Visitar URL original
ipaddress: support slicing for accessing network addresses and hosts · Issue #111931 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20250616223152/https://github.com/python/cpython/issues/111931
Skip to content

ipaddress: support slicing for accessing network addresses and hosts #111931

Open
@mazunki

Description

@mazunki

Feature or enhancement

Proposal:

Intuitively, I thought this would work, but it fails with a TypeError when comparing a slice with an int.

import ipaddress

net = ipaddress.IPv4Network("10.5.5.0/24")
for ip_addr in net[:35]:
    print(ip_addr)

Currently, _BaseNetwork doesn't support slicing, because the class doesn't implement the __len__ method, for good reason: it could cause a confusion between the number of addresses in the network, or the bitsize for the address.

Because of this, I believe we should implement support for slicing directly into the getter, and potentially extending the support for this into the hosts() function of the same class.

I've created a working implementation for this.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions