> That sounds like a good idea. I'm certainly getting concerned about
> the proliferation of methods that people "should" add to file-like
> objects, where read/write are the only fundamental ones needed.
> 
> I can't see mixins working, as too many file-like objects are written in C...

One could use "class decorators". For example if you want to define the
method foo() in a file-like class, you could use code like:

def FooExtender(cls):
  class wrapper(cls):
    pass
  try:
    # Don't do anything if "foo" already defined
    wrapper.foo
  except AttributeError:
    def foo(self):
      """ Automatically generated foo method. """
      self.write("foo\n")
    wrapper.foo = foo
  return wrapper

MyFileClass = FooExtender(MyCFileClass)

This is for classes, but the construct can be adapted to work on objects
instead.

The advantage of using a decorator-like function is that you can do some
complex processing in the function (you could for example automatically
define __ne__ if only __eq__ is defined, and conversely). And it could
probably plug, as an useful convenience or even an automatic mechanism,
into a more sophisticated adaptor system.



_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to