# Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

# PythonElement is added to a list when it shouldnt

#### Velpus Captiosus

##### Well-Known Coder
Hello everyone.Only part of the Node class is relevant to my question:

Python:
``````import math

class Node:
prevNodes  = []
b = 0.5
def __init__(self,value,nextNode):
self.value = 0
if nextNode is not None:
self.nextNode = nextNode
self.nextNode.prevNodes.append(self)

def changeB(self,error):
if self.b+error>0:
self.b+=error
else:
print("Weight cannot be < 0")

def printVal(self):
print(self.value,"\n")

def changeVal(self,inp):
self.value = inp
self.nextNode.value += float(self.value)* self.b

class Network:
def __init__(self,nodes):
self.nodes = nodes

def calcOutput(self):
finalNode = self.nodes
while finalNode.nextNode is not None:
finalNode = self.nodes.nextNode

return 1-pow(math.e,-(finalNode.value))

def returnOutputNode(self):
finalNode = self.nodes
while finalNode.nextNode is not None:
finalNode = finalNode.nextNode

return finalNode

def digitize(self,val):
if val<0.5:
val = 0
else:
val = 1
return val

def correctBiases(self,node,error):
print(node.value)
node.changeB(error)
if len(node.prevNodes) != 0:
for nd in node.prevNodes:
self.correctBiases(nd,error)

def checkAnswer(self):
y = self.digitize(self.calcOutput())
print(y,"\n")
x = float(input("What is the correct output?\n"))
if x != y:
z = (self.digitize(self.calcOutput())-x)/10
print(z,'\n')
self.correctBiases(self.returnOutputNode(),z)

y1 = Node(0,None)
x1  = Node(0,y1)
print(len(y1.prevNodes))
print(len(x1.prevNodes))``````

However `len(x1.prevNodes)` returns a value of 1 and I don't understand how this is possible. When I programmed the class I thought it would have returned 0. And I want it to be 0 because x1 doesn't have any previous nodes? Any ways I can fix this?

Last edited by a moderator:
Solution
Hey there,

A bit of a head-scratcher, isn’t it? Let's roll up our sleeves and dig into this little enigma together. Diving into your code, it seems like the key part we need to decipher lies within the Node class and its prevNodes attribute.

Python, being the smooth operator it is, handles class variables in a bit of a quirky way. Your prevNodes list is defined as a class variable, and thus, it gets shared across all instances of Node, acting almost like a communal storage space for all nodes - a collective memory, if you will.

Here’s a quick fix:Let's make prevNodes an instance variable by initializing it within the init method, ensuring each Node gets its very own personal space to remember its predecessors. 🧠✨

Python:
``class...``
Hey there,

A bit of a head-scratcher, isn’t it? Let's roll up our sleeves and dig into this little enigma together. Diving into your code, it seems like the key part we need to decipher lies within the Node class and its prevNodes attribute.

Python, being the smooth operator it is, handles class variables in a bit of a quirky way. Your prevNodes list is defined as a class variable, and thus, it gets shared across all instances of Node, acting almost like a communal storage space for all nodes - a collective memory, if you will.

Here’s a quick fix:Let's make prevNodes an instance variable by initializing it within the init method, ensuring each Node gets its very own personal space to remember its predecessors. 🧠✨

Python:
``````class Node:
b = 0.5

def __init__(self, value, nextNode):
self.value = 0
self.prevNodes = [] # Aha! Our personal memory space!

if nextNode is not None:
self.nextNode = nextNode
self.nextNode.prevNodes.append(self)``````

With this tweak, every new Node instance gets its own cozy prevNodes list instead of all nodes gossiping and sharing one communal list. 😉📜

Quickly scanning through the rest of the provided code, nothing peculiar jumped out at me, but if you catch another snag or two down the line, feel free to holler! Happy coding, and may your bug-hunting be ever prosperous! 🐛🏹💻

Hey there,

A bit of a head-scratcher, isn’t it? Let's roll up our sleeves and dig into this little enigma together. Diving into your code, it seems like the key part we need to decipher lies within the Node class and its prevNodes attribute.

Python, being the smooth operator it is, handles class variables in a bit of a quirky way. Your prevNodes list is defined as a class variable, and thus, it gets shared across all instances of Node, acting almost like a communal storage space for all nodes - a collective memory, if you will.

Here’s a quick fix:Let's make prevNodes an instance variable by initializing it within the init method, ensuring each Node gets its very own personal space to remember its predecessors. 🧠✨

Python:
``````class Node:
b = 0.5

def __init__(self, value, nextNode):
self.value = 0
self.prevNodes = [] # Aha! Our personal memory space!

if nextNode is not None:
self.nextNode = nextNode
self.nextNode.prevNodes.append(self)``````

With this tweak, every new Node instance gets its own cozy prevNodes list instead of all nodes gossiping and sharing one communal list. 😉📜

Quickly scanning through the rest of the provided code, nothing peculiar jumped out at me, but if you catch another snag or two down the line, feel free to holler! Happy coding, and may your bug-hunting be ever prosperous! 🐛🏹💻
Omg!It acts like a static variable in Java?(I have done Java at my university but never have been good at it , nor do I remember much)I didnt know that! Thank you so much!Have a good day!

Aha, you got it! The Python class variable is indeed playing in the same ballpark as the static variables in Java. Tricky little things, aren’t they?

Super thrilled that the explanation hit the mark! 🎯 Your kind words and lightbulb moment totally made my day, so a big thank you from over here!

Tiny favor to ask - if you could give that tick button 🟩 a little click beside my previous message, it’d mark this thread as solved. Helps to guide fellow code-explorers who might tumble into the same conundrum in the future. Plus, we all do a little happy dance when these coding mysteries get unraveled! 🕺💃

Looking forward to our next coding adventure! Until then, happy coding and have yourself a brilliant day!