How do Java interfaces translate to Python?

Lee Morgan unknown at
Mon Oct 1 03:37:47 CEST 2001

Gordon Tyler <gordon at> writes:

> In article <j44rpk2unm.fsf at>, Martin von Loewis
<loewis at> wrote: 
> > What exactly is it that you do with interfaces in Java?
> Enforcing a contract between objects, i.e. if you want to work with this
> object, your object must implement this interface.
> I find myself unable to think of anything other than that. Which suggests
> to me that perhaps interfaces aren't such a useful construct after all. ;)
> I suppose the thing I'm really missing is compile-time type safety. Yes I
> know that Python is a dynamically typed language and as such compile-time
> type safety, compiling even, make no sense. But do you have any
> suggestions for somebody used to compiled languages on how to deal with a
> dynamically typed language like Python? I find myself making silly
> syntactical mistakes which I just don't see when I visually scan the code,
> and when I run the program it suddenly barfs in the middle of nowhere with
> a syntax error. Very frustrating. Especially, if it takes a few minutes
> and interaction from me to get to that point.

Yeah, I've often got caught out by this myself. Also silent name bindings to
classes (which you sometimes want) got me recently.

There is a PEP for an interface enhancement, but its a still a dynamic
enforcement, so instead of 

Traceback (innermost last):
  File "<stdin>", line 1, in ?
An object has failed to implement interface FishMarketInterface

you would get

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AttributeError: 'm' instance has no attribute 'getFishMonger'

which to my mind isn't an improvement.

If interfaces aren't checked at byte-compile time surely they just become a code
documentation feature - perhaps useful to utils like pychecker but unlikely to
get regular use.

(Granted I've only just started looking at Interfaces in python as I wanted to
stop getting bitten in the arse, even if they were my teeth - but interface's as
suggested in the PEP don't seem to address the problem)

Some wistful folks on this list purport the idea that python programmers need
to know what they're doing so they don't need any sort ot type checking. Whilst
this could well be true, under the hot iron of commercial development you need
to come back to old code, quickly understand it, and be confident in making
changes. Yes you could unit test extensively, but some situations make this
tricky (gui apps for example).

Anyway, sorry about the rant - I wanted interfaces to help me with the same
problem you're having and I'm confused they wont. I'd hate to loose Pythons
dynamic nature, but sometimes I want a little support.

But, given python's lisp like dynamism you can enforce your own with varying
degrees of success. One caveat - I'm still playing with ways to do this as my
understanding of python grows and haven't found one I really like. Also the
recently added module can help here.

# This class raises error if attr not one set in init func
# It won't help complex objects, ie when __setattr__ isnt called
class _base:
    initialising = 1
    def __init__(self):
        self.attrONE = 1
        self.initialising = 0
    def __str__(self):
        return str(self.attrONE) + str(self.initializing)
    def __setattr__(self,attr,value):
        if self.initialising:
            self.__dict__[attr] = value            
            if self.__dict__.has_key(attr):
                self.__dict__[attr] = value
                raise self.__class__.__name__ + " does not have attr " + attr
    def foo(self):

me = _base()
me.attrONE = 3
me.attrONE = 5
me.attrOEN = 4	# error raised

# check some attr
def enforceInterface(instance, requiredAttr = ['foo','foo2']):
  for attr in requiredAttr:
    if not hasattr(instance,attr):
      raise instance.__class__.__name__ + " is missing attribute " + attr

#check at top level
enforceInterface(me) # error on foo2

Lee Morgan

More information about the Python-list mailing list