Python namedtuple: 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'

>Solution :

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))

Leave a Reply