python名为tuple:attributeError:' tuple'对象没有属性' end_pos'

发布于 2025-01-31 05:40:11 字数 1608 浏览 2 评论 0原文

我的课程开始如下:

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>

编辑:我调用方法添加 lhsRHSpos 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤千羽 2025-02-07 05:40:11

您可以尝试一下:从本质上讲,您将其传递给了一个类元组,而不是称为规则的 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))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文