0

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

5
  • Why are you nesting the class? What advantage does that give you? Commented Dec 11, 2020 at 13:09
  • "# when looking at port_1 i see that it is the same as port_0" well, of course it is. You only ever create a single Net.Port instance... inside the __init__ of Net, which you instantiate here: net_0 = Net("Net_10"). Then, you simply assign that instance to two different names, port_0 and port_1. Commented Dec 11, 2020 at 13:11
  • Well, I started with nested Dictionary but it went complicated. my desire is to create the hierarchical DB as I pass over the file. I have a bout 3M line in a file, so I don't want to go over it again & again with each loop for every NEt and PORT's within it. I thought that if I create an obj for each NET and then OBJ's for each PORT, I will succeed to capture all instances in one sweep. If you can suggest on another way, you are more then welcome (-: Commented Dec 11, 2020 at 16:15
  • I think you misunderstood me, I'm asking why are you nesting the Port class inside the Net class? That doesn't make any sense. Just bring Port outside into the same level as Net. Commented Dec 11, 2020 at 16:43
  • I'm not an expert, so I 'm open to ideas...I appreciate your effort. I tried to eliminate the nested part and as you mentioned here, I generated two classes that solve the issue. The code is very short and I was leaning on a similar solution from: GerryDevine , stackoverflow.com/questions/54057560/…. Thanks for the help (-: Commented Dec 11, 2020 at 16:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.