python名为tuple:attributeError:' tuple'对象没有属性' end_pos'
我的课程开始如下:
from collections import namedtuple
class Parser:
Rule = namedtuple('Rule', ['lhs', 'rhs', 'dot_pos', 'start_pos', 'end_pos'])
# __init__ ...
由于Pycharm通过给我适当的建议正确地检测到了我所有的元组元素命名,所以我以为我以正确的方式做到了,创建一个小rule
如上所示。
现在,我在Parser
类中有一个方法,该类别rule
参数:
def add(self, dot_rule: Rule):
print(dot_rule.end_pos)
# ...
不幸的是,一旦我尝试调用dot_rule
dot_rule <的元素,以下错误就会出现。 /code>喜欢
end_pos
:
attributeError:'tuple'对象没有属性'end_pos'
在使用nequ> nequ> nequ> nequ> nation>
编辑:我调用方法添加
lhs
,RHS
和pos
pos 代码>是提前计算的一些值:
self.add((LHS,RHS,0,POS,POS)))
我想,因为据说nequ>
元组
这将是正确的语法。显然,该参数现在被视为普通元组
,而不是Rule
。我在这里可以做什么?
编辑2:追溯消息:
Traceback (most recent call last):
File "...\earley.py", line 19, in <module>
main()
File "...\earley.py", line 14, in main
parser = Parser(grammar, lexicon, sentence)
File "...\parser.py", line 21, in __init__
self.parse(sentence)
File "...\parser.py", line 56, in parse
self.predict('S', i)
File "...\parser.py", line 41, in predict
self.add((lhs, rhs, 0, pos, pos)) # (X -> .α, i, i)
File "...\parser.py", line 24, in add
print(dot_rule.end_pos)
AttributeError: 'tuple' object has no attribute 'end_pos'
I have a class that starts as follows:
from collections import namedtuple
class Parser:
Rule = namedtuple('Rule', ['lhs', 'rhs', 'dot_pos', 'start_pos', 'end_pos'])
# __init__ ...
Since PyCharm detected all my tuple element namings correctly by giving me proper suggestions, I assumed I did it the right way so far, creating a little Rule
class with the syntax shown above.
Now, I have a method within my Parser
class that takes a Rule
parameter:
def add(self, dot_rule: Rule):
print(dot_rule.end_pos)
# ...
Unfortunately, following error appears as soon as I try to call an element of dot_rule
like end_pos
:
AttributeError: 'tuple' object has no attribute 'end_pos'
What is it that I misunderstood when using namedtuple
?
Edit: I call the method add
the following way with lhs
, rhs
, and pos
being some values calculated beforehand:
self.add((lhs, rhs, 0, pos, pos))
I thought since namedtuple
is said to be backward-compatible with tuple
this would be the correct syntax. Apparently, the parameter is now seen as a plain tuple
instead of a Rule
. What can I do differently here?
Edit 2: Traceback message:
Traceback (most recent call last):
File "...\earley.py", line 19, in <module>
main()
File "...\earley.py", line 14, in main
parser = Parser(grammar, lexicon, sentence)
File "...\parser.py", line 21, in __init__
self.parse(sentence)
File "...\parser.py", line 56, in parse
self.predict('S', i)
File "...\parser.py", line 41, in predict
self.add((lhs, rhs, 0, pos, pos)) # (X -> .α, i, i)
File "...\parser.py", line 24, in add
print(dot_rule.end_pos)
AttributeError: 'tuple' object has no attribute 'end_pos'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试一下:从本质上讲,您将其传递给了一个类元组,而不是称为规则的 nationtuple 。请参阅:
而不是
self.add((LHS,RHS,0,POS,POS)))
使用
self.add(解析器) .rule(LHS,RHS,0,POS,POS))
You can try this: essentially you were passing it a class tuple instead of the namedtuple called Rule. See:
Instead of
self.add((lhs, rhs, 0, pos, pos))
Use
self.add(Parser.Rule(lhs, rhs, 0, pos, pos))