[Python-Dev] Revive the types sig?
Ka-Ping Yee
ping@lfw.org
Sun, 11 Mar 2001 21:18:06 -0800 (PST)
On Sun, 11 Mar 2001, Michel Pelletier wrote:
> As I see it, interfaces satify your first point, remove the need for your
> second and third point, satify your fourth point, and meet the goals of
> your fifth.
For the record, here is a little idea i came up with on the
last day of the conference:
Suppose there is a built-in class called "Interface" with the
special property that whenever any immediate descendant of
Interface is sub-classed, we check to make sure all of its
methods are overridden. If any methods are not overridden,
something like InterfaceException is raised.
This would be sufficient to provide very simple interfaces,
at least in terms of what methods are part of an interface
(it wouldn't do any type checking, but it could go a step
further and check the number of arguments on each method).
Example:
>>> class Spam(Interface):
... def islovely(self): pass
...
>>> Spam()
TypeError: interfaces cannot be instantiated
>>> class Eggs(Spam):
... def scramble(self): pass
...
InterfaceError: class Eggs does not implement interface Spam
>>> class LovelySpam(Spam):
... def islovely(self): return 1
...
>>> LovelySpam()
<LovelySpam instance at ...>
Essentially this would replace the convention of writing a
whole bunch of methods that raise NotImplementedError as a
way of describing an abstract interface, making it a bit easier
to write and causing interfaces to be checked earlier (upon
subclassing, rather than upon method call).
It should be possible to implement this in Python using metaclasses.
-- ?!ng
"Computers are useless. They can only give you answers."
-- Pablo Picasso