Class variables

Besides instance variables, there are also class variables. They are are created by specifying variables inside the class itself, not a method:

class Network:
    all_allocated_ip = []

    def __init__(self, network): = network
        self.hosts = tuple(
            str(ip) for ip in ipaddress.ip_network(network).hosts()
        self.allocated = []

    def allocate(self, ip):
        if ip in self.hosts:
            if ip not in self.allocated:
                raise ValueError(f"IP address {ip} is already in the allocated list")
            raise ValueError(f"IP address {ip} does not belong to {}")

Class variables can be accessed in different ways:

  • self.all_allocated_ip
  • Network.all_allocated_ip
  • type(self).all_allocated_ip

The self.all_allocated_ip option allows you to access the value of class variable or add an element if the class variable is a mutable data type. The disadvantage of this option is that if in the method you write self.all_allocated_ip = ..., instead of changing the class variable, an instance variable will be created.

The option Network.all_allocated_ip will work correctly, but a small drawback this option is that the class name is written manually. You can use the third option type(self).all_allocated_ip instead, since type(self) returns a class.

Now the class has a variable all_allocated_ip which is written all IP addresses that are allocated on the networks:

In [3]: net1 = Network("")

In [4]: net1.allocate("")
   ...: net1.allocate("")
   ...: net1.allocate("")

In [5]: net1.allocated
Out[5]: ['', '', '']

In [6]: net2 = Network("")

In [7]: net2.allocate("")
   ...: net2.allocate("")

In [9]: net2.allocated
Out[9]: ['', '']

In [10]: Network.all_allocated_ip
Out[10]: ['', '', '', '', '']

The variable is accessible not only through the class, but also through the instances:

In [40]: Network.all_allocated_ip
Out[40]: ['', '', '', '', '']

In [41]: net1.all_allocated_ip
Out[41]: ['', '', '', '', '']

In [42]: net2.all_allocated_ip
Out[42]: ['', '', '', '', '']