我正在為 replit 上的神經元制作一個小專案。
我做了一個名為Neurons. 我設定connections = []。我創建了一個函式,呼叫connect()它通過突觸連接兩個神經元。如果一個神經元觸發,connections串列中的所有其他神經元都會收到信號。
我怎樣才能讓同一類中的兩個不同物件相互通信,以便神經元知道他們的合作伙伴之一是否剛剛向他們開火?
這是代碼:
class Neuron:
def __init__(self, type='interneuron'):
self.type = type
self.connections = []
def connect(self, neuron):
self.connections.append(neuron)
def receive(self): pass
def fire(self): pass
n1 = Neuron()
n2 = Neuron('motor')
n1.connect(n2)
這個replit的鏈接在這里:https ://replit.com/@EmmaGao8/Can-I-make-Neurons#main.py 。
感謝您的時間和考慮。
uj5u.com熱心網友回復:
您可以遍歷所有其他神經元并執行該receive功能。
例如:
def fire(self):
for other in self.connections:
other.receive() #and whatever other things you want
def receive(self):
print("Signal Received!") #or whatever else you want
如果這對您有幫助,請接受此答案以供將來參考(單擊復選標記),如果沒有,請告訴。
uj5u.com熱心網友回復:
當您使用類的connect()方法Neuron添加n2到 的connections串列中時n1,您正在創建實體之間的鏈接。
如果您列印n1.connetions和n2,您會看到它們指向記憶體中的同一個物件。因此,您可以將fire()方法與receive()方法一起定義如下:
def receive(self): pass
def fire(self):
# do something
for n in self.connections:
n.receive()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/377542.html
