Skip to content

Unnecessary operation in IPv4Interface and IPv6Interface #134012

Not planned
@mssalvatore

Description

@mssalvatore

The __hash__() methods of the IPv4Interface and IPv6Interface classes are implemented on the main branch as follows:

    def __hash__(self):
        return hash((self._ip, self._prefixlen, int(self.network.network_address)))

https://github.com/python/cpython/blob/0afbd4e42ac28240b484cabe1710fdb6c572fb1f/Lib/ipaddress.py#L1457C1-L1459

As best as I can figure from tracing the relevant __init__() methods, self._ip and int(self.network.network_address) will always be identical. If that is so, this function could be simplified and save a few CPU cycles:

     def __hash__(self):
-        return hash((self._ip, self._prefixlen, int(self.network.network_address)))
+        return hash((self._ip, self._prefixlen))

Linked PRs

Activity

added a commit that references this issue on May 14, 2025

pythongh-134012: Simplify IPv*Interface.__hash__()

mssalvatore

mssalvatore commented on May 15, 2025

@mssalvatore
ContributorAuthor

I missed some details and the conclusion that self._ip and self.network.network_address are always identical are incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Unnecessary operation in IPv4Interface and IPv6Interface · Issue #134012 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant