[英]why does it say that my class doesn't have the attribute?
here's my code: 这是我的代码:
class Regex:
def __init__(self: 'Regex',
value: object =None, child: list =None, child2: list = None):
"""node with value and any nummber of children"""
list_of_regex = [0,1,2,'e','|','.','*']
self.value = value
if self.value not in list_of_regex:
raise Exception
if not child:
self.child = []
if not child2:
self.child2 = []
else:
self.child = child[:]
self.child = child2[:]
def __repr__(self: 'Regex') -> str:
return ('Regex(' + repr(self.value) + ', ' +
repr(self.child) + ', ' + repr(self.child2) + ')')
when i input the following in my python shell: 当我在我的python shell中输入以下内容时:
>>>Regex(1,[1])
the output is: 输出是:
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 19, in __repr__
builtins.AttributeError: 'Regex' object has no attribute 'child'
I'm not sure why it tells me that my class doesn't have the attribute 'child'. 我不确定为什么它告诉我我的班级没有属性'child'。 Look what happens when i try these two different calls.
看看当我尝试这两个不同的电话时会发生什么。
>>>c = Regex(1)
>>>c.child
[]
>>>c = Regex(1,[1])
>>>c.child
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
builtins.AttributeError: 'Regex' object has no attribute 'child'
what's wrong with the code? 代码有什么问题?
if not child:
self.child = []
if not child2:
self.child2 = []
else:
self.child = child[:]
self.child = child2[:]
The else
block here is linked to the second if
. 这里的
else
块链接到第二个if
。 It has no connection to the first if
. 它与第一个
if
无关。 That means if child
is specified and child2
is not, the second if
block is the only one that triggers, and child
isn't set. 这意味着如果指定了
child
而child2
不是,则第二个if
块是唯一触发的块,并且未设置child
。
Also, 也,
self.child = child2[:]
you probably wanted to set the child2
attribute here. 你可能想在这里设置
child2
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.