Feb-15-2019, 04:23 AM
(This post was last modified: Feb-15-2019, 04:26 AM by ScottDiesing.)
I thought this code would produce the following:
nic_id 1 has 5 ports
nic_id 2 has 2 ports
but instead it produces:
nic_id 3 has 5 ports
nic_id 3 has 2 ports
Is there a way to assign the current value of the class variable Count to the instance variable nic_id?
Here is the code:
nic_id 1 has 5 ports
nic_id 2 has 2 ports
but instead it produces:
nic_id 3 has 5 ports
nic_id 3 has 2 ports
Is there a way to assign the current value of the class variable Count to the instance variable nic_id?
Here is the code:
class NIC:
Count = 0
def __init__(self, num_ports):
self.nic_id = NIC.Count
self.num_ports = num_ports
NIC.Count += 1
def displayNIC(self):
print ("nic_id %d has %d ports" % (NIC.Count, self.num_ports))
nic1 = NIC(5)
nic2 = NIC(2)
print (nic1.displayNIC())
print (nic2.displayNIC())
