Define an InterfaceΒΆ

Define an interface by subclassing jute.Opaque.

import jute

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

Subclassing jute.Opaque indicates that this class is an interface. The interface requires implementations to provide the attributes defined in the class definition (write) and in the super-class. jute.Opaque is an interface containing no attributes. Hence, the Writable interface only requires the attribute write.

Interfaces can be subclassed further to add more attributes:

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

Interface BufferedWritable requires implementations to provide both write and flush attributes.

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

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)

Interfaces can define non-method attributes using the jute.Attribute class:

class BufferedWritableFile(BufferedWritable):

    fd = jute.Attribute("The file descriptor of the file to be written", type=int)