4

I have created the following objects in python

It creates an object of class arc and then creates another object network using the object arc

class Arc:
    # Creates an object of class arc
    def __init__(self, tailNode = 0, headNode = 0, lowerBound = 0, upperBound = 0, cost = 0):
        self.tailNode = tailNode
        self.headNode = headNode
        self.lowerBound = lowerBound
        self.upperBound = upperBound
        self.cost = cost
    def displayArc(self):
        print("Tail Node : ", self.tailNode,  "\nHead Node : ", self.headNode, "\nLower Bound : ", self.lowerBound, "\nUpper Bound ; ", self.upperBound, "\nCost : ", self.cost, "\n")

class Node:
    # Create an object of class node
    def __init__(self, nodeName = 0, distLabel = 0, preNode = 0):
        self.nodeName = nodeName
        self.distLabel = distLabel
        self.preNode = preNode

class Network:
     # Creates a network from given arcs
     def __init__(self, fileName):
        global arcNo 
        arcNo = 0

        self.fileName = fileName
        f = open(self.fileName)
        x = f.readlines()

        arcList = [ Arc() for i in range(len(x))]

    for i in range(len(x)):
        temp = x[i]
        temp = temp.split("\n")
        temp = ",".join(map(str, temp))
        temp = temp.split(",")
        arcList[i] = Arc(temp[0], temp[1], temp[2], temp[3], temp[4])
        arcNo += 1

    print(arcNo)

net = Network("arcList.txt")
print(type(net))
print(net[1])

When the print statement comes says

4
<class '__main__.Network'>
Traceback (most recent call last):
      File "Dijkstra.py", line 54, in <module>
    print(net[1])
TypeError: 'Network' object does not support indexing

How do I support indexing so that I can call the network object by its index ?

5
  • what do you expect to see when typing print net[1]? Commented Aug 2, 2016 at 5:13
  • I was hoping to see a list of all the attributes given to it like 1,2,0,100,6 Commented Aug 2, 2016 at 7:38
  • On a dumber note, in the initialisation is the arcList created assigned to net ? Commented Aug 2, 2016 at 7:50
  • no, right now, arcList can only be accessed from your __init__() method. If you need to be part of your class instance (net), you need to write self.arcList = .... You can then call net.arcList Commented Aug 2, 2016 at 7:56
  • Thanks a lot. It's working now Commented Aug 2, 2016 at 8:04

2 Answers 2

3

Assuming net[index] returns the arcList variable you can simply override your [] operator as such

class Network:
    def __getitem__(self,index):
         return arcList[index]

Printing a class needs a method as well. This might help you How to print a class or objects of class using print()?

Sign up to request clarification or add additional context in comments.

1 Comment

I want to access the class. Print is only for checking. For example if I give a=net[1], I want a string or array assigned to "a".
1

In order to support indexing, your Network class should have a __getitem__() method (http://docs.python.org/reference/datamodel.html#object.getitem).

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.