[Python-3000] Abilities / Interfaces

Antoine Pitrou solipsis at pitrou.net
Mon Nov 20 20:39:55 CET 2006


Le lundi 20 novembre 2006 à 10:27 -0800, Guido van Rossum a écrit :
> I'd like to have a discussion of abilities and interfaces but right
> now I don't have time -- maybe tomorrow. I see Andrew's proposal as
> mostly isomorphic to Zope's notion of interfaces, but the new
> terminology may make it possible to clarify certain distinctions
> better, e.g. the distinction between a class that provides an ability
> to its instances, and the abilities that an instance has.
> 
> Feel free to riff on this theme here. I'll check in within 24 hours or so.


It doesn't directly answer your post, but I think the way an
object/class is queried to know whether it implements a given interface
should be quite free-form, which means it's up to the interface creator
to decide what really he wants to check.

For example, the creator of the Number interface may just check that
Number is declared as an interface, without going any further. So, the
author of Complex class just has to write:

class Complex:
    __implements__ = (Number,)

An interface creator who has a taste for "stricter" typing may on the
other hand mandate that certain or attributes methods are defined, etc.
He could then raise an error if a class claiming to implement his
interface doesn't provide the required methods/attributes.


So to sum it up, to know whether an object-or-class implements a given
interface, you could call a standard function
implements(obj, interface), which in turn would call
interface.__implemented_by__(obj). It's up to each interface to decide
what breadth of tests it wants to perform, although it would be
practical to fallback on a default implementation (in the Interface base
class common to all interfaces) which just checks for presence of the
interface in obj.__implements__.


(I don't know if this post is useful. Sorry if it's not)

Antoine.




More information about the Python-3000 mailing list