cpython
cpython copied to clipboard
Make `IPv{4,6}Address.version` and `max_prefixlen` available on classes, not just instances
Feature or enhancement
Proposal:
Sometimes, when there is a function that can operate both on IPv4 and IPv6 addresses, it is convenient to pass it the corresponding type object (either IPv4Address
or IPv6Address
) as an argument. However, that function cannot access the properties version
and max_prefixlen
using those type objects, because they only work on instances. This can be worked around by constructing a dummy instance (e.g. t(0).max_prefixlen
), but it would be more convenient if those properties could be used on the type object directly (i.e. t.max_prefixlen
), because their value doesn't depend on the specific instance, only on the type.
The proposal is to make IPv4Address.version
, IPv4Address.max_prefixlen
, IPv6Address.version
, and IPv6Address.max_prefixlen
evaluate to 4, 32, 6, and 128, respectively. Currently, all those expressions evaluate to property descriptors.
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
This seems reasonable to me.
While there is a tiny downside of these technically becoming writable even though they're supposed to be read only attributes... this is Python and what you ask for seems practical. Agreed, lets do it.
While there is a tiny downside of these technically becoming writable even though they're supposed to be read only attributes...
They don't become any more writable then they already are. Currently, they can be assigned on the type objects but not on the instances. This will remain. For example, in the current version of Python, it is possible to write IPv4Address.version = 5
, and then the version
of all IPv4Address
instances will be 5
.