python - 新类实例继承以前的实例值
问题描述
我正在编写一个实现双向链表的小树类:
class node(object):
def __init__(self, level=0, pieces=0, parent=None,
root=None, childNo=None, avgN=2):
self.level = level # Current level in the tree. Zero = top.
self.pieces = pieces # Number of children. Can be constant or Poisson chosen random.
self.parent = parent # Parent of this node.
self.child = {} # Children of this node. Using a dictionary means
# the root could have direct access\
# to all children.
self.childrenID = {}
self.myID = childNo # This node's index in the parents self.child list.
if (root == None): # If I'm the root, then hey, I'm the root!
self.root = self
else:
self.root = root
self.avgN = avgN
self.numOfChildren = 0
self.pieces = self.avgN
def print_all_data(self):
print "Printing all data for node:",self.myID
obj_attr = [a for a in dir(n) if not a.startswith('__') and not callable(getattr(n,a))]
for ob in obj_attr:
print ob, getattr(n,ob)
return
# This function acutally adds a child node to this parent.
def add_child_node(self, childno):
self.numOfChildren += 1
if (self.numOfChildren > self.pieces):
print "Error: number of children exceeds the number of assigned pieces"
print " for node:", self.myID
childno = self.compute_new_child_ID(self.level, self.numOfChildren)
self.childrenID[self.numOfChildren] = childno
self.child[childno] = self.get_new_node(childno)
return
def get_new_node(self,childno):
return node(level=self.level+1, parent=self,
root=self.root, childNo=childno,
avgN=self.avgN)
def compute_new_child_ID(self, level, childno):
return (level+1)*100 + childno
def get_child_no_from_child_ID(self, level, childID):
return childID - (level+1)*100
请注意,有些功能看起来是重复的,但这些是供将来使用的占位符。
现在,如果我创建一个实例,我会得到:
n = node()
n.print_all_data()
Printing all data for node: None
avgN 2
child {}
childrenID {}
level 0
myID None
numOfChildren 0
parent None
pieces 2
root <__main__.node object at 0x7fb33dc0ae10>
但是,添加任何子项会导致它们继承父项的属性:
n.add_child_node(0)
n.add_child_node(1)
n.print_all_data()
Printing all data for node: None
avgN 2
child {101: <__main__.node object at 0x7fb33dc46350>, 102: <__main__.node object at 0x7fb33dc460d0>}
childrenID {1: 101, 2: 102}
level 0
myID None
numOfChildren 2
parent None
pieces 2
root <__main__.node object at 0x7fb33dc0ae10>
这是孩子的数据:
n.child[101].print_all_data()
Printing all data for node: 101
avgN 2
child {101: <__main__.node object at 0x7fb33dc46350>, 102: <__main__.node object at 0x7fb33dc460d0>}
childrenID {1: 101, 2: 102}
level 0
myID None
numOfChildren 2
parent None
pieces 2
root <__main__.node object at 0x7fb33dc0ae10>
同样创建一个新节点会继承这个旧的实例数据:
n2 = node()
n2.print_all_data()
Printing all data for node: None
avgN 2
child {101: <__main__.node object at 0x7fb33dc46350>, 102: <__main__.node object at 0x7fb33dc460d0>}
childrenID {1: 101, 2: 102}
level 0
myID None
numOfChildren 2
parent None
pieces 2
root <__main__.node object at 0x7fb33dc0ae10>
现在我已经搜索了整个 StackOverflow,并且我没有使用 init(这些是实例属性),也没有在函数定义中不正确地初始化不可变字典(在此处进一步解释)。我在 SO 中找不到与我的问题相匹配的任何其他示例,因此是新问题。非常感谢任何帮助。
解决方案
实例是正确的,您只是在打印错误的数据。
在方法print_all_data
中,您总是打印变量引用的实例的n
属性(它是从更高的范围继承的,并且无论在什么实例中使用它总是引用您的“根”节点)而不是打印当前实例的属性(self
参考)。
在方法print_all_data
中,您需要这样的东西(当前实例由变量引用self
):
obj_attr = [a for a in dir(self)
if not a.startswith('__') and not callable(getattr(self, a))]
推荐阅读
- python - Anaconda 发行说明中的“PATH env var 的缩减值”是什么意思?
- javascript - 如何使用 clsx 有条件地渲染 css?
- angular - 带有嵌套字段的 Angular8 和 formControlName
- xcode - 在 Xcode 下的 OpenCL 源代码中递归包含文件
- r - 我不能有一个真正的背靠背条形图
- python - 如何在 Windows 中使用 Python 对齐新墙纸
- mysql - Mysql关于获取最大最小值和电流的问题
- javascript - 使用 Javascript 在 Wordpress 页面上自动刷新 2 张图片
- c# - 插入数据库时用字符创建序列号
- c++ - 我们如何在 c++ 中使用三角函数时纠正 nan