重写 __repr__ 时出错'TypeError-expected 0 arguments, got 1' [英] Error while rewriting __repr__ 'TypeError-expected 0 arguments, got 1'
问题描述
我在通过弄清楚 __repr__
的用法来调试以下代码时出错.
I have an error while debugging the following code by figuring out the usage of __repr__
.
class node(object):
def __init__(self, value):
self.value = value
self.children = []
def __repr__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__repr__(level+1)
return ret
def add(self, nod):
self.children.append(nod)
leaf_1 = [1,4,3]
leaf_2 = [2,5,3]
leaf_3 = [4,4,3]
leaf_4 = [5,5,5]
tree = parent = node(leaf_1)
parent.add(leaf_2)
parent.add(leaf_3)
parent.add(leaf_4)
print(tree) # no error without this line
每当我输入 print(tree)
时,都会出现错误,但是如果我注释了这一行,则没有错误.如果我将 self.children.append**(nod)**
更改为 self.children.append**(node(nod))**
,则不会出错使用 print(tree)
.
Whenever I put the print(tree)
, there is an error, but if I commented this line, no error. If I change self.children.append**(nod)**
to self.children.append**(node(nod))**
, no error while I use print(tree)
.
Traceback (most recent call last):
File "C:\Users....tree.py", line 30, in <module>
print(tree)
File "C:\Users....tree.py", line 9, in __repr__
ret += child.__repr__(level+1)
TypeError: expected 0 arguments, got 1
问题:
- 为什么我会收到错误消息?我在哪里可以看到
1
参数? - 为什么它必须使用
self.children.append**(node(nod))**
?我完全不明白这背后的逻辑?
- why did I get the error? where can I see the
1
argument? - why it must be using the
self.children.append**(node(nod))**
? I totally do not understand the logic behind it?
我已经调试并查看了值,但我只能看到变量的更新和关系,我看不到原因.如果有人能向我解释一下,我将不胜感激,谢谢!
I have debugged and watched values, but I can only see the variables' updates and relations, I cannot see the reasons. I much appreciate it if anyone can explain a little bit to me, thanks!
推荐答案
我尝试回答您的问题:
你的第一个错误:
Traceback (most recent call last):
File "C:\Users....tree.py", line 30, in <module>
print(tree)
File "C:\Users....tree.py", line 9, in __repr__
ret += child.__repr__(level+1)
TypeError: expected 0 arguments, got 1
这是因为在您的班级中,您定义了以下内容:
This is because in your class, you've defined the following:
def __repr__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__repr__(level+1)
return ret
level=0
代表一个 kwarg 参数.如果需要使用该参数,则需要执行以下操作:ret += child.__repr__(level=level+1)
The level=0
represents a kwarg argument. If you need to use that argument, you need to do the following:
ret += child.__repr__(level=level+1)
如果你只想做 ret += child.__repr__(level+1)
,你的 __repr__
定义应该是:
If you had wanted just to do ret += child.__repr__(level+1)
, your __repr__
definition should be:
def __repr__(self, level):
然后调用 ret += child.__repr__(level+1)
不会报错.
Then calling ret += child.__repr__(level+1)
would not give an error.
现在,关于 self.children.append**(node(nod))**
;
如您的代码所述,leaf_* 是列表.
As stated in your code, leaf_* are lists.
leaf_1 = [1,4,3]
leaf_2 = [2,5,3]
leaf_3 = [4,4,3]
leaf_4 = [5,5,5]
您的 def add(self, nod):
方法只是将 nod
的值附加到 self.children 列表中.但是,在所有附加之后, self.children
将只是一个列表列表,而不是您在执行以下操作时所期望的节点列表:
Your def add(self, nod):
method just appends the value of nod
to the self.children list. However, after all that appending, self.children
would be just a list of lists, not a list of nodes which is what your expecting when you do the following:
for child in self.children:
ret += child.__repr__(level+1)
因为你定义了 Node 对象的 repr 方法,如果每个孩子都是一个 Node
对象,那么上面的方法是有效的;但他们不是.
Since you defined the Node object's repr method, the above would be valid if each child were of a Node
object; but they aren't.
这就是为什么当您执行 self.children.append(Node(nod))
时,实际上是将 Node 类型的实例化对象附加到 self.children
领域.
This is why when you do a self.children.append(Node(nod))
, you are actually appending instantiated objects of Node type to the self.children
field.
总而言之,最终的结果应该是:
So all in all, the final result should be:
class node(object):
def __init__(self, value):
self.value = value
self.children = []
def __repr__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__repr__(level=level+1)
return ret
def add(self, nod):
self.children.append(nod)
leaf_1 = [1,4,3]
leaf_2 = [2,5,3]
leaf_3 = [4,4,3]
leaf_4 = [5,5,5]
tree = parent = node(leaf_1)
parent.add(node(leaf_2))
parent.add(node(leaf_3))
parent.add(node(leaf_4))
print(tree)
或者你当然可以做你以前做过的事情:
or you can, of course do what you did before:
class node(object):
def __init__(self, value):
self.value = value
self.children = []
def __repr__(self, level=0):
ret = "\t"*level+repr(self.value)+"\n"
for child in self.children:
ret += child.__repr__(level=level+1)
return ret
def add(self, nod):
self.children.append(node(nod))
leaf_1 = [1,4,3]
leaf_2 = [2,5,3]
leaf_3 = [4,4,3]
leaf_4 = [5,5,5]
tree = parent = node(leaf_1)
parent.add(leaf_2)
parent.add(leaf_3)
parent.add(leaf_4)
这篇关于重写 __repr__ 时出错'TypeError-expected 0 arguments, got 1'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!