keeping state in an iterator object by rebinding next()

Wilbert Berendsen wbsoft at xs4all.nl
Wed Mar 19 15:36:22 EDT 2008


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.

>>> class A:
...  def a(self):
...   self.next = self.b
...   return 1
...  def b(self):
...   self.next = self.a
...   return 2
...  def __iter__(self):
...   return self
...
>>> a=A()
>>> a.a()
1
>>> a.next()
2
>>> a.next()
1
>>> j=0
>>> for i in a:
...  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

-- 
http://www.wilbertberendsen.nl/
"You must be the change you wish to see in the world."
        -- Mahatma Gandi



More information about the Python-list mailing list