Define an InterfaceΒΆ

Define an interface by subclassing jute.Interface.

import jute

class Writable(jute.Interface):
    def write(self, buf):
        """Function to write a string."""

Interfaces can be subclassed:

class BufferedWritable(Writable):
    def flush(self):
        """Flush any pending output."""

Interface BufferedWritable requires both write and flush attributes to be provided.

Objects can provide the same syntactical interface, but different semantics. These differences can be represented by interface hierarchies with no additional syntax:

class LineBufferedWritable(BufferedWritable):
    """
    No additional operations, but indicates the semantic information
    that the buffer is flushed when a newline occurs.
    """

# need line buffering here
if LineBufferedWritable.supported_by(out):
    out.write(buf)
else:
    # more expensive operation
    add_line_buffering(out, buf)