[英]Logic gate classes - how exactly does the flow of execution work here?
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. 我有一个
LogicGate
类,可以有一个或两个输入行(分别是UnaryGate
和BinaryGate
子类),还有一个Connector
类,它包含两个LogicGate
对象,并且有一个将一个门的输出返回到另一个门的方法,因此接收端的门可以对其执行操作。 What I'm having trouble understanding is what object is actually performing the get_from()
method here: 我无法理解的是什么对象实际上在这里执行
get_from()
方法:
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? 在
get_pin()
方法中self.pin.get_from().get_output()
到底在做什么? 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. 如果执行
get_pin()
的UnaryGate
是Connector
的togate
,则get_from()
返回相应的fromgate
,后者依次执行get_output()
并根据从其连接的一两个门接收到的输入返回1
或0
。 。 What I can't wrap my head around is the self.pin.get_from()
part. 我无法确定的是
self.pin.get_from()
部分。 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
? 被
get_from()
被调用的上pin
对象,我的理解就是一个类的属性,可以是1
, 0
或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. 现在,我了解了他们如何执行该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.