Hrvoje Niksic hniksic at xemacs.org
Thu Nov 22 16:37:04 CET 2007

braver <deliverable at gmail.com> writes:

> On Nov 22, 6:10 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> Granted, they aren't part of the stdlib - but then, lots
>> of things aren't.
> As Hendrik noticed, I can't even add my own f.eof() if I want to
> have buffering -- is that right?

You can, you just need to inherit from built-in file type.  Then
instances of your class get the __dict__ and with it the ability to
attach arbitrary information to any instance.  For example:

class MyFile(file):
  def __init__(self, *args, **kwds):
    file.__init__(self, *args, **kwds)
    self.eof = False

  def read(self, size=None):
      if size is None:
          val = file.read(self)
          self.eof = True
          val = file.read(self, size)
          if len(val) < size:
              self.eof = True
      return val

  def readline(self, size=None):
      if size is None:
          val = file.readline(self)
          val = file.readline(self, size)
      if len(val) == 0:
          self.eof = True
      return val

The code needed to support iteration is left as an excercise for the

More information about the Python-list mailing list