I am learning about object oriented programming from
Problem solving with Algorithms and Data structures
def __init__(self, fgate, tgate):
self.fromgate = fgate
self.togate = tgate
You are missing two things here: implied function parameters and duck typing.
Firstly, the syntax
tgate.setNextPin(self) calls the function
setNextPin(tgate, self). The first parameter is the object for which the method is being called (usually named self). You are getting confused because we are passing
self as the second parameter to the function
setNextPin but the first parameter of that function is also called
self. This is a scoping issue, those two
selfs refer to different parameters (depending on which function you are in).
Secondly, there is duck typing. How does the function know it can access the method in the
BinaryGate class? It doesn't know that at all! All the function knows is that it expects
tgate to be of a type that has a method
setNextPin. It doesn't matter if this is a
BinaryGate or any other object. If the object passed doesn't have this method to call then Python will fail at run time as it will be unable to find the function
setNextPin to call.