MCPcopy Index your code
hub / github.com/python/cpython / __init__

Method __init__

Lib/ipaddress.py:1499–1549  ·  view source on GitHub ↗

Instantiate a new IPv4 network object. Args: address: A string or integer representing the IP [& network]. '192.0.2.0/24' '192.0.2.0/255.255.255.0' '192.0.2.0/0.0.0.255' are all functionally the same in IPv4. Similarly,

(self, address, strict=True)

Source from the content-addressed store, hash-verified

1497 _address_class = IPv4Address
1498
1499 def __init__(self, address, strict=True):
1500 """Instantiate a new IPv4 network object.
1501
1502 Args:
1503 address: A string or integer representing the IP [& network].
1504 '192.0.2.0/24'
1505 '192.0.2.0/255.255.255.0'
1506 '192.0.2.0/0.0.0.255'
1507 are all functionally the same in IPv4. Similarly,
1508 '192.0.2.1'
1509 '192.0.2.1/255.255.255.255'
1510 '192.0.2.1/32'
1511 are also functionally equivalent. That is to say, failing to
1512 provide a subnetmask will create an object with a mask of /32.
1513
1514 If the mask (portion after the / in the argument) is given in
1515 dotted quad form, it is treated as a netmask if it starts with a
1516 non-zero field (e.g. /255.0.0.0 == /8) and as a hostmask if it
1517 starts with a zero field (e.g. 0.255.255.255 == /8), with the
1518 single exception of an all-zero mask which is treated as a
1519 netmask == /0. If no mask is given, a default of /32 is used.
1520
1521 Additionally, an integer can be passed, so
1522 IPv4Network('192.0.2.1') == IPv4Network(3221225985)
1523 or, more generally
1524 IPv4Interface(int(IPv4Interface('192.0.2.1'))) ==
1525 IPv4Interface('192.0.2.1')
1526
1527 Raises:
1528 AddressValueError: If ipaddress isn't a valid IPv4 address.
1529 NetmaskValueError: If the netmask isn't valid for
1530 an IPv4 address.
1531 ValueError: If strict is True and a network address is not
1532 supplied.
1533 """
1534 addr, mask = self._split_addr_prefix(address)
1535
1536 self.network_address = IPv4Address(addr)
1537 self.netmask, self._prefixlen = self._make_netmask(mask)
1538 packed = int(self.network_address)
1539 if packed & int(self.netmask) != packed:
1540 if strict:
1541 raise ValueError('%s has host bits set' % self)
1542 else:
1543 self.network_address = IPv4Address(packed &
1544 int(self.netmask))
1545
1546 if self._prefixlen == (self.max_prefixlen - 1):
1547 self.hosts = self.__iter__
1548 elif self._prefixlen == (self.max_prefixlen):
1549 self.hosts = lambda: iter((IPv4Address(addr),))
1550
1551 @property
1552 @functools.lru_cache()

Callers

nothing calls this directly

Calls 3

IPv4AddressClass · 0.85
_split_addr_prefixMethod · 0.80
_make_netmaskMethod · 0.45

Tested by

no test coverage detected