2

I am learning about object oriented programming from

Problem solving with Algorithms and Data structures book.

I have trouble understanding the setNextPin(self,source) method in Connector Class. I understand that the tgate.setNextPin(self) mean tgate.setNextPin(tgate,self) but what really is self here ?? It is confusing with me what the self of Connector stand for ?? Logically, this method will set tgate.PinA = self, and becaause we want tgate to take value from fgate so self == fgate ? Sorry if the question is stupid or confusing

class Connector:

    def __init__(self, fgate, tgate):
        self.fromgate = fgate
        self.togate = tgate

        tgate.setNextPin(self)

    def getFrom(self):
        return self.fromgate

    def getTo(self):
        return self.togate

I think I understand the basic idea of self, but if we have:

g1 = AndGate("G1") 
g2 = AndGate("G2") 
g3 = OrGate("G3") 
c1 = Connector(g1,g3) 

So in this case which is g3.setNexPin(self) will set the next pin of g3 to c1 ?? but c1 is only a connector and donot hold any value ??

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Anh Hoang
  • 79
  • 8
  • Possible duplicate of [What is the purpose of self in Python?](http://stackoverflow.com/questions/2709821/what-is-the-purpose-of-self-in-python) – Code-Apprentice Aug 17 '16 at 18:52
  • 2
    Yes, `g3.setNextPin(self)` would set "next" pin of `g3` to `c1`. As for the next sentence, could you please elaborate what do you mean by "but c1 is only a connector and donot hold any value ??", as I am not familiar with this book. – Antti Haapala -- Слава Україні Aug 17 '16 at 19:13
  • ah sorry, I mean the pin of g3 must be a value right. g3 is a OrGate class which take two value, for example False or False and return the value False. What I means is c1 do not hold any value so how can we set pin of g3 to c1 – Anh Hoang Aug 17 '16 at 19:17
  • @AnhHoang Be careful to keep straight the difference between the variables `g3` and `c1` in your Python code and the behavior of the objects to which they refer. – Code-Apprentice Aug 17 '16 at 19:18
  • From what you have shown here, no Boolean values are stored anywhere. However, you haven't posted any code for your gate classes. It is probably easiest to think of the `Connector` class as representing the wire which connects the pins for the gates on a breadboard. – Code-Apprentice Aug 17 '16 at 19:21
  • ah, I finally understand the logic here, thanks. – Anh Hoang Aug 17 '16 at 19:31

2 Answers2

1

self refers to the "current" object. In __init__() this is the object which is being created and initialized. So tgate.setNextPin(self) sets the next pin of tgate to the current Connector. It is not fgate as you guessed. It is the new Connector which is being initialized.

For more details, see What is the purpose of self?

So in this case which is g3.setNexPin(self) will set the next pin of g3 to c1 ??

That is basically correct. A small nitpick: The actual code is tgate.setNextPin(self). In this particular case, tgate refers to the same object as g3 and self refers to the object that is being created. After __init__() returns, c1 will be assigned to refer to this newly created object.

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I think I understand the basic idea, but if we have: g1 = AndGate("G1") g2 = AndGate("G2") g3 = OrGate("G3") c1 = Connector(g1,g3) so in this case which is g3.setNexPin(self) will set the next pin of g3 to c1 ?? but c1 is only a connector and donot hold any value ?? – Anh Hoang Aug 17 '16 at 19:03
  • @AnhHoang You should consider editing your question if you have further details...especially code since you cannot format well it in a comment. – Code-Apprentice Aug 17 '16 at 19:04
  • I have edited the question to be more clear, please help – Anh Hoang Aug 17 '16 at 19:08
  • here is the link to the chapter with full code at the end http://interactivepython.org/runestone/static/pythonds/Introduction/ObjectOrientedProgramminginPythonDefiningClasses.html – Anh Hoang Aug 17 '16 at 19:19
1

I just stumbled on this question reading this book. I found the answer when I tried to execute the entire active code given at the end of the section. In the BinaryGate Class, the getPinA() returns

'self.pinA.getFrom().getOutput()'

So breaking down -

'self.pinA --> c1' 
'c1.getFrom() --> g1'
'g1.getOutput() --> gets the output of gate g1'

This getPinA() is used to send the input to the binary gate and thus it makes sense to provide the connector as the source to tgate.setNextPin()

link for reference: https://runestone.academy/runestone/books/published/pythonds/Introduction/

Pranith K
  • 11
  • 1