qz_99
qz_99

Reputation: 175

Using a list as a class parameter

class Interface():
    def __init__(self, localIP, remoteIP, numHops, hops):
        self.localIP = localIP
        self.remoteIP = remoteIP
        self.numHops = numHops
        self.hops = []

I want to create an instance like this:

hops, hopIps = stripRoute(ssh.run("traceroute -I " + str(dstHost.ip), hide=True))
host.interfaces.append(Interface(host.ip, dstHost.ip, hops, hopIps))
print(hops)
print(hopIps)

From the print statements I can see that hopIps has a value and length 1 which is expected. However when I then query the new instance of Interface, only the numHops value was updated, hops remains empty.

Upvotes: 1

Views: 73

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

You passed a list into __init__ but never used it

class Interface():
    def __init__(self, localIP, remoteIP, numHops, hops):
        self.localIP = localIP
        self.remoteIP = remoteIP
        self.numHops = numHops
        self.hops = []

Just assign your list to the member

class Interface():
    def __init__(self, localIP, remoteIP, numHops, hops):
        self.localIP = localIP
        self.remoteIP = remoteIP
        self.numHops = numHops
        self.hops = hops

Upvotes: 6

Related Questions