Hi, I have just joined python-dev and I saw your very interesting
proposal
for implementing intefaces.
> I have an idea for an interface mechnism for Python, and I'd like to
see if
> anyone likes it before writing an actual PEP. [...]
I like it a lot! Anyway, if it can be implemented in python as is, what
is
the point of the PEP? Making the 'interface' root class and/or
InterfaceError
builtins, maybe?
I have some comments which I thought I would bounce. I'll organize these
attending to the activities they relate to. Don't hesitate to tell me if
I'm
sayig something stupid. :)
Define an interface
===================
In your example, it seems that
class Foo(interface):
def default_foo(self, a, b):
"Docstring for foo method."
print "Defaults can be handy."
[I added the a, b arguments to illustrate one point below]
Does the following:
- Defines the requirement that all objects that implement Foo have a
foo
function
- Defines that foo should accept arguments of the form (a, b) maybe?
- Sets a doc string for the foo method.
- Sets a default implementation for the method.
Some questions on this:
- Can an interface only define method names (and maybe argument
formats)?
I think it would be handy to let it expose attributes.
- Is method names (and maybe format of arguments) the only thing you
can
'promise' in the interface? In other words, is that the only type of
guarantee that code that works against the interface can get? I think
a __check__(self, obj) special method in interfaces would be a simple
way to boost their flexibility.
- For the uses you have given to the prefix_methodname notation so far,
I don't think it's really needed. Isn't the following sufficient?
class Foo(interface):
def foo(self, a, b):
"foo docstring"
# nothing here; no default definition
def bar(self, a, b):
pass # no docstring, _empty definition_
This has the side effect that a method with no default definition and no
doc string is a SyntaxError. Is this too bad?
- It would maybe be hard to figure out what such a method is supposed to
do, so you _should_ provide a docstring.
- If you're in a hurry, an empty docstring will do the trick. While in
'quick and dirty mode' you probably won't be using interfaces a lot,
anyway.
Defaults look indeed useful, but the really crucial aspect to lay down
in an interface definition is what does it guarantee on the objects that
implement it. If amalgamating this with default defs would otherwise
obscure it (there's another issue I'm addressing below), I think
defaults
belong more properly to a class that implements the interface, not to
the
interface definition itself.
I guess knowing what other uses you have in mind for the
prefix_methodname
notation could be useful to decide whether it's warranted.
Check whether an object implements an interface
===============================================