I have a LogicGate
class, which can have one or two input lines ( UnaryGate
and BinaryGate
subclasses, respectively) and a Connector
class, which contains two LogicGate
objects and has a method that returns one gate's output to another so the gate at the receiving end can perform operations on it. What I'm having trouble understanding is what object is actually performing the get_from()
method here:
class LogicGate:
def __init__(self,n):
self.label = n
self.output = None
def get_label(self):
return self.label
def get_output(self):
self.output = self.perform_gate_logic()
return self.output
class UnaryGate(LogicGate):
def __init__(self,n):
LogicGate.__init__(self,n)
self.pin = None
def get_pin(self):
if self.pin == None:
pin = int(input("Enter Pin input for gate " + self.get_label() + "-->"))
while pin != 1 and pin != 0:
pin = int(input("Try again with 1 or 0 -->"))
return pin
else:
return self.pin.get_from().get_output()
def set_next_pin(self,source):
if self.pin == None:
self.pin = source
else:
raise RuntimeError("ERROR: NO EMPTY PINS")
class Connector:
def __init__(self,fgate,tgate):
self.fromgate = fgate
self.togate = tgate
tgate.set_next_pin(self)
def get_from(self):
return self.fromgate
def get_to(self):
return self.togate
What exactly is self.pin.get_from().get_output()
doing in the get_pin()
method? If the UnaryGate
performing get_pin()
is the Connector
's togate
, then get_from()
returns the corresponding fromgate
, which in turn performs get_output()
and returns 1
or 0
based on the input it received from the one or two gates it's connected to. What I can't wrap my head around is the self.pin.get_from()
part. Is get_from()
being invoked on a pin
object, which to my understanding is just a class attribute that can be 1
, 0
or None
? That doesn't make much sense to me as I don't see how it could perform the method. How exactly is the data 'transferred' between a gate and the gate(s) it's connected to?
When the gate hasn't received any input from the user, the pins are actually connectors, as shown by this interpreter session:
>>> g1 = AndGate("g1")
>>> g2 = AndGate("g2")
>>> g3 = OrGate("g3")
>>> g4 = NotGate("g4")
>>> c1 = Connector(g1,g3)
>>> c2 = Connector(g2,g3)
>>> c3 = Connector(g3,g4)
>>> repr(g4)
'<__main__.NotGate object at 0x102958890>'
>>> g4.pin
<__main__.Connector object at 0x10297f450>
>>> g4.pin == c3
True
>>> g3.pin_a
<__main__.Connector object at 0x10297f590>
>>> g3.pin_a == c1
True
>>> g3.pin_b == c2
True
>>> g1.pin_a
>>> print(g1.pin_a)
None
>>>
Now I understand how they can perform the method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.