0

I'm banging my head against my desk trying tu understand what I'm doing wrong.

class Endpoint:
    PortID = 0
    MAC = ''
    IP = ''
    FQDN = ''
    __init__()...


class Interface:
    Index = 0
    Endpoints = []
    __init__()
    ...

im main section i have:

for i in interfaces: #list of Interface objects
    for e in endpoints: #list of Endpoint objects
        is (some condidions):
            i.Endpoints.append(e)
    i.print() # prints all endpoints connected to the interface

Then in the output I have something like this:

ge0/0:
  11:11:11:11:11:11
ge0/1:
  11:11:11:11:11:11
  22:22:22:22:22:22
ge0/2
  11:11:11:11:11:11
  22:22:22:22:22:22
  33:33:33:33:33:33

where what I intend to get is:

ge0/0:
  11:11:11:11:11:11
ge0/1:
  22:22:22:22:22:22
ge0/2
  33:33:33:33:33:33

You get the point. It's like the "i" variable isn't created and destroyed in every loop iteration, but is rather just updated. In other words it's not a representation of a Interface object, but rather some kind of buffer commot to a whole loop. What am I doing wrong? How can I achieve desired goal?

Thank you in advance for your help.

Zbig
  • 3
  • 2
  • Another, possibly better target: [How to avoid having class data shared among instances?](https://stackoverflow.com/questions/1680528/how-to-avoid-having-class-data-shared-among-instances) – Ilja Everilä Jun 05 '18 at 08:45

1 Answers1

0

In your main function:

for i in interfaces: #list of Interface objects
for e in endpoints: #list of Endpoint objects
    is (some condidions):
        i.Endpoints.append(e)
    i.print() # prints all endpoints connected to the interface

You are not clearing previous value of i

Add i = 0 after printing (or any statement to clear value of i), as follows:

for i in interfaces: #list of Interface objects
    for e in endpoints: #list of Endpoint objects
        is (some condidions):
            i.Endpoints.append(e)
    i.print() # prints all endpoints connected to the interface
    i = 0
akash
  • 587
  • 4
  • 16