I am making a little project for Neurons on replit.
I made a class called Neurons
. I set connections = []
.
I created a function called connect()
to connect two Neurons via synapses.
If one neuron fires, all of the other Neurons in the connections
list would receive the signal.
How could I make two different Objects in the same Class communicate with each other, so that the Neurons would know if one of their partners just fired to them?
Here is the code:
JavaScript
x
17
17
1
class Neuron:
2
def __init__(self, type='interneuron'):
3
self.type = type
4
self.connections = []
5
6
def connect(self, neuron):
7
self.connections.append(neuron)
8
9
def receive(self): pass
10
11
def fire(self): pass
12
13
14
n1 = Neuron()
15
n2 = Neuron('motor')
16
n1.connect(n2)
17
This link to the replit is here: https://replit.com/@EmmaGao8/Can-I-make-Neurons#main.py.
Thank you for your time and consideration.
Advertisement
Answer
You can go through all of the other Neurons and execute the receive
function.
For example:
JavaScript
1
7
1
def fire(self):
2
for other in self.connections:
3
other.receive() #and whatever other things you want
4
5
def receive(self):
6
print("Signal Received!") #or whatever else you want
7