Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm pretty new to OOP.

I'm trying to simulate cars moving along a road in a graph-like fashion. Each Road object has a source and destination. When a car reaches the end of the road, I want the road to send it to the beginning of the next road. My code looks as follows for the Road class:

from collections import deque

class Road:
    length = 10

    def __init__(self, src, dst):
        self.src = src
        self.dst = dst
        self.actualRoad = deque([0]*self.length,10)
        Road.roadCount += 1

    def enterRoad(self, car):
        if self.actualRoad[0] == 0:
            self.actualRoad.appendleft(car)
        else:
            return False

    def iterate(self):
        if self.actualRoad[-1] == 0:
            self.actualRoad.appendleft(0)
        else:
            dst.enterRoad(actualRoad[-1]) #this is where I want to send the car in the last part of the road to the destination road!

    def printRoad(self):
        print self.actualRoad

testRoad = Road(1,2)
testRoad.enterRoad("car1")
testRoad.iterate()

In the code above, the problem is at the else part of the method iterate(): How do I call another Object's method from the current Object's Method? Both methods are in the same class.

Thank you VERY much for your help in advance.

Daniel

share|improve this question

2 Answers 2

It seems to me that you are confusing the difference between class and object.

A class is the piece of code where you model an object by specifying the attributes that compose its and the methods that defines its behaviors. In this case, the Road class.

In the other hand, an object is nothing else but an instance of the class that defines it. Therefore it has a state which is defined by the values of it attributes. Again, in this case, the testRoad is variable that stores an object of the Road class.

In one word, while the class is an abstract model, the object is a concrete instance with a well defined state.

So then when you say that you want:

call another Object's method from the current Object's Method

what you are actually wanting is to define in your class a method that allows you to call another method from an object of the same class.

Then, to do so, the class method needs to receive as parameter the object from which you want to invoke whatever method you want to invoke:

def iterate(self, destination_road):
        if self.actualRoad[-1] == 0:
            self.actualRoad.appendleft(0)
        else:
            destination_road.enterRoad(actualRoad[-1])
share|improve this answer

You must give the other Object as a parameter to iterate:

def iterate(self, other):
    ...

And to call a method from that object:

other.someMethod(...)
share|improve this answer
    
Thank you for answering. I'm wondering if this is the best implementation, since I realise now that each car reaching the end of the road may be going to a different road. What, in your opinion, would be the best implementation for that? The roads are structured using a linked list. –  Daniel van Flymen Nov 23 '13 at 20:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.