W
Wilbert Berendsen
Hi,
i am writing a simple parser, that generates tokens. The parser needs to
maintain some state, because some parts of the file consist of different
tokens. I thought the object could simply remember its state by assigning
it's next() method to the method that is currently parsing. When the state
changes, the called method rebinds next() and the next token will be returned
by that function. Here's an example, proving that this indeed works.
.... def a(self):
.... self.next = self.b
.... return 1
.... def b(self):
.... self.next = self.a
.... return 2
.... def __iter__(self):
.... return self
........ j += 1
.... if j > 10: break # prevent from running endlessly
.... print i
....
2
1
2
1
2
1
2
1
2
1
my question is: is this legal Python? An iterator could save the next() method
object, and in that case it could stop working.... It works now, because
apparently the for- construct resolves 'next' each time for the object before
calling it.
The other solution would be just jumping to the correct method from within the
next() method. But that gives an extra call...
Met vriendelijke groet,
Wilbert Berendsen
i am writing a simple parser, that generates tokens. The parser needs to
maintain some state, because some parts of the file consist of different
tokens. I thought the object could simply remember its state by assigning
it's next() method to the method that is currently parsing. When the state
changes, the called method rebinds next() and the next token will be returned
by that function. Here's an example, proving that this indeed works.
.... def a(self):
.... self.next = self.b
.... return 1
.... def b(self):
.... self.next = self.a
.... return 2
.... def __iter__(self):
.... return self
........ j += 1
.... if j > 10: break # prevent from running endlessly
.... print i
....
2
1
2
1
2
1
2
1
2
1
my question is: is this legal Python? An iterator could save the next() method
object, and in that case it could stop working.... It works now, because
apparently the for- construct resolves 'next' each time for the object before
calling it.
The other solution would be just jumping to the correct method from within the
next() method. But that gives an extra call...
Met vriendelijke groet,
Wilbert Berendsen