0

im a networking guy trying to learn some python to be able to automate. im having a difficult time grasping OOP. wrote the script below (not perfect i would assume) to experiment, and i keep getting the following as output:

main.Router object at 0x0000021C68033E80

my script:

class Router:
    name=''
    def __init__(self,ip,network):
        self.ip = ip
        self.network=network

    def getip(self):
        print(self.ip)
    def getname(self):
        print(self.name)
    def getnetwork(self):
        print(self.network)

class Switch(Router):
    def __init__(self,ip,network,layer):
        Router.__init__(self,ip,network)
        self.layer=layer
        self.vlans=[]
    def addvlan(self,vlan):
        self.vlans.append(vlan)
    def getvlans(self):
        print(self.vlans)
    def getlayer(self):
        print(self.layer)

routers=[]
switches=[]
while(True):
    rors = ""
    while(rors != "router" and rors!= "switch" and rors != "0"):
        rors = input("router or switch? 0 to exit: ")
    if(rors=="router"):
        routers.append(Router(input("enter ip: "),input("enter network: ")))
    elif(rors=="switch"):
        switches.append(Switch(input("enter ip: "),input("enter network: "),input("layer?: ")))
        stop=1
        while(stop!=0):
            stop=int(input("enter 1 to continue, 0 to stop adding vlans: "))
            if(stop==0):break
            switches[len(switches)-1].addvlan(int(input("add vlan: ")))
    elif(rors == "0"):
        break
    else: print("input error")

print(switches)
print(routers)

i understand that by printing the lists i just print where the memory pointer is pointing, but how do i make it so the values themselves are the ones actually being printed? thanks for the help!

4
  • 2
    you probably want to override either the __str__ or __repr__ method Commented Jan 23, 2021 at 23:59
  • 2
    @dangee1705: Given they're printing lists of the objects, definitely __repr__; list always prints the repr of the contained objects. Commented Jan 24, 2021 at 0:02
  • @ShadowRanger yeah that sounds about right, I can never remember the difference between the two Commented Jan 24, 2021 at 0:08
  • List != Array in python. You have lists Commented Jan 24, 2021 at 2:39

1 Answer 1

2

You need to override the __repr__ method to change the string representation of the objects. Here's an example:

In [1]: class A1:
   ...:     def __init__(self, a, b):
   ...:         self.a = a
   ...:         self.b = b
   ...:

In [2]: class A2:
   ...:     def __init__(self, a, b):
   ...:         self.a = a
   ...:         self.b = b
   ...:     def __repr__(self):
   ...:         kvps = [f"{k}={v}" for k, v in vars(self).items()]
   ...:         return f"{type(self).__name__}({', '.join(kvps)})"
   ...:

In [3]: A1(1, 2)
Out[3]: <__main__.A1 at 0x7f533357fe20>

In [4]: A2(1, 2)
Out[4]: A2(a=1, b=2)
Sign up to request clarification or add additional context in comments.

Comments

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.