树数据结构__str__方法
Tree data structure __str__ method
class TreeNode:
def __init__(self,data,children = []):
self.data = data
self.children = children
def __str__(self,level=0):
ret = " " * level + str(self.data) + '\n'
for child in self.children:
ret += child.__str__(level+1)
return ret
# adding the children to the tree node
def addchildren(self,TreeNode):
self.children.append(TreeNode)
问题 1:请解释 def __str__(self,level=0):
。特别是child.__str__(level+1)
drinks = TreeNode('Drinks',[])
cold = TreeNode('Cold',[])
hot = TreeNode('Hot',[])
cola = TreeNode('Cola',[])
cappucino = TreeNode('Cappucino',[])
drinks.addchildren(cold)
drinks.addchildren(hot)
cold.addchildren(cola)
hot.addchildren(cappucino)
print(drinks)
问题 2:还有一件事,如果我使用 self.children.append(TreeNode.data)
,为什么会出现这种类型的错误(如下所示),我知道它不会起作用,但为什么print( ) 语句抛出此错误但不在 self.children.append(TreeNode)
中。为什么说 expected 0 arguments, got 1 ?
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_944/4195955341.py in <module>
----> 1 print(drinks)
~\AppData\Local\Temp/ipykernel_944/3676504849.py in __str__(self, level)
8 ret = " " * level + str(self.data) + '\n'
9 for child in self.children:
---> 10 ret += child.__str__(level+1)
11
12 return ret
TypeError: expected 0 arguments, got 1
def __str__(self,level=0):
ret = " " * level + str(self.data) + '\n'
for child in self.children:
ret += child.__str__(level+1)
return ret
" " * level
表示重复space关卡次数。 level
的默认值是 object 本身的 0
和 children 的级别加一,这些 children 再次调用他们的 __str__
children 等级提升1。所以当前 object 在行首有 0 spaces,它的行首有 children 1 space,childrens 28=]ren 2 spaces 在他们行的开头等等,这提供了很好的视觉表示,类似于您在浏览目录时可能遇到的情况,例如:
rootdir
dir1
dir11
dir12
dir2
dir21
dir22
其中 dir11 和 dir12 在 dir1 内,dir21 和 dir22 在 dir2 内,dir1 和 dir2 在 rootdir 内。
python __str__
方法有超过 1 个参数(任何超过 self
的参数)并不常见。假设 child 中的一个是元组 (1,2,3)
那么当你尝试打印你的树时,它会尝试打印级别等于 1
的树,即
(1,2,3).__str__(1)
class TreeNode:
def __init__(self,data,children = []):
self.data = data
self.children = children
def __str__(self,level=0):
ret = " " * level + str(self.data) + '\n'
for child in self.children:
ret += child.__str__(level+1)
return ret
# adding the children to the tree node
def addchildren(self,TreeNode):
self.children.append(TreeNode)
问题 1:请解释 def __str__(self,level=0):
。特别是child.__str__(level+1)
drinks = TreeNode('Drinks',[])
cold = TreeNode('Cold',[])
hot = TreeNode('Hot',[])
cola = TreeNode('Cola',[])
cappucino = TreeNode('Cappucino',[])
drinks.addchildren(cold)
drinks.addchildren(hot)
cold.addchildren(cola)
hot.addchildren(cappucino)
print(drinks)
问题 2:还有一件事,如果我使用 self.children.append(TreeNode.data)
,为什么会出现这种类型的错误(如下所示),我知道它不会起作用,但为什么print( ) 语句抛出此错误但不在 self.children.append(TreeNode)
中。为什么说 expected 0 arguments, got 1 ?
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_944/4195955341.py in <module>
----> 1 print(drinks)
~\AppData\Local\Temp/ipykernel_944/3676504849.py in __str__(self, level)
8 ret = " " * level + str(self.data) + '\n'
9 for child in self.children:
---> 10 ret += child.__str__(level+1)
11
12 return ret
TypeError: expected 0 arguments, got 1
def __str__(self,level=0):
ret = " " * level + str(self.data) + '\n'
for child in self.children:
ret += child.__str__(level+1)
return ret
" " * level
表示重复space关卡次数。 level
的默认值是 object 本身的 0
和 children 的级别加一,这些 children 再次调用他们的 __str__
children 等级提升1。所以当前 object 在行首有 0 spaces,它的行首有 children 1 space,childrens 28=]ren 2 spaces 在他们行的开头等等,这提供了很好的视觉表示,类似于您在浏览目录时可能遇到的情况,例如:
rootdir
dir1
dir11
dir12
dir2
dir21
dir22
其中 dir11 和 dir12 在 dir1 内,dir21 和 dir22 在 dir2 内,dir1 和 dir2 在 rootdir 内。
python __str__
方法有超过 1 个参数(任何超过 self
的参数)并不常见。假设 child 中的一个是元组 (1,2,3)
那么当你尝试打印你的树时,它会尝试打印级别等于 1
的树,即
(1,2,3).__str__(1)