I'm trying to create an Hierarchical data structure with nested classes. I have a file that has several NET's and each NET has several PORT's. So I want to go over the file in one swap and collect all data at once. I tried the following code:
"""Parent Class to all PORTS children classes"""
def __init__(self, name):
self.name = name
self.port = Net.Port(x_cord = None)
def display(self):
print("Inside Net Display")
print("Net name:",self.name)
def reveal(self):
## Calling the inner class Port
self.port.port_display("Calling inner class Port from outer class Net")
def show_classes(self):
print("Net class:", self.display())
print("Port class:",self.port.display())
class Port:
"""Inner class in Net class"""
def __init__(self, x_cord):
self.x_cord = x_cord
def display(self):
print("Inside Port display")
print(self.x_cord)
So now I create the NET class and the port instances:
net_0 = Net("Net_10")
port_0 = net_0.port
port_1 = net_0.port
# initializing port_0
port_0.x_cord = 5
port_0.display()
# output:
Inside Port display
5
# when looking at port_1 i see that it is the same as port_0
port_1.display()
Inside Port display
5
what actually happened is that I created two port instances, but they were the same one. port_0 <main.Net.Port at 0x27649b40d60> port_1 <main.Net.Port at 0x27649b40d60>
how can I create two different sub cells of port?
thanks, GB
Net.Portinstance... inside the__init__ofNet, which you instantiate here:net_0 = Net("Net_10"). Then, you simply assign that instance to two different names,port_0andport_1.Portclass inside theNetclass? That doesn't make any sense. Just bringPortoutside into the same level asNet.