Tree node structure:
Код: Выделить всё
class BPlusNode:
def __init__(self,isleaf = True,val=[],parent=None,children=[]):
self.values = val
self.children = children
self.parent = parent
self.isleaf = isleaf
self.order=3
Код: Выделить всё
def split(self,node):
if (len(node.children)==0 and len(node.values) 5):
sys.exit()
print('',new_root.values)
node.parent=new_root
self.root=new_root
parent=new_root
the output is:
the output
strangely the new node has already been assigned values
but the program runs correctly if I write like this:
Код: Выделить всё
new_root=BPlusNode(isleaf=False,val=[],parent=None,children=[])
I noticed a mechanism of default parameter in python:
operations on mutable objects with default parameters will be preserved
for example:
Код: Выделить всё
cnt=2
def f(data=[]):
global cnt
data.append(1)
print(data)
data[0]=cnt
cnt+=1
return data
f()
f()
f()
Код: Выделить всё
[1]
[2, 1]
[3, 1, 1]
Код: Выделить всё
def __init__(self,isleaf = True,val=[],parent=None,children=[]):
Is this problem related to this mechanism?
Or it is caused by other reasons?
Источник: https://stackoverflow.com/questions/781 ... ing-python