ungetch in Python

Gabriel Genellina gagenellina at softlab.com.ar
Wed Dec 24 14:40:10 EST 2003


At 24/12/2003 11:41, you wrote:

>I'm writing a small parser for a minilanguage in Python,
>and I was wondering --- is there any equiv. of C's ungetch
>or Scheme's peek-char in python? That is, is there a way to

No, and neither a getc(), but you can make them easily (provided you just 
ungetc *one* char at a time)
(Untested):

class file_with_ungetc(file):
   ungetbuffer = None
   def ungetc(self, c):
     self.ungetbuffer = c
   def getc(self):
     if self.ungetbuffer is not None:
       c=self.ungetbuffer
       self.ungetbuffer = None
       return c
     else:
       return self.read(1)


Gabriel Genellina
Softlab SRL






More information about the Python-list mailing list