PEP 245: Python interfaces
Michel Pelletier
michel at digicool.com
Mon Mar 26 17:50:29 EST 2001
On 26 Mar 2001, Dave LeBlanc wrote:
> Having read the PEP briefly, I have one question:
>
> Is there a good reason to add the "implements" keyword?
>
> Current syntax is (from Language Reference 7.6):
> classdef: "class" classname [inheritance] ":" suite
> inheritance: "(" [expression_list] ")"
> classname: identifier
> (i'm adding this based on the text of 7.6:)
> expression_list: Expression(s) which evaluate to a class object.
>
> Any reason why expression_list couldn't include an interface?
Because that expression list defines inheritance, and interfaces are not
inherited. They do not define any of the same behaviors, like shared
implementation, as inheritance. Readers of your code would (wrongly)
assume that you were mixing in implementation into your class instead of
asserting the implementation of a certain interface.
> Seems to
> me it's easier to just extend the semantics of expression_list to
> include interface_name(s) instead of adding a variation like '"class"
> classname [inheritance] ["implements" interfaces] ":" suite'.
> (Besides, two optional elements in a row makes for messy parsing :-).)
You mean human visual parsing or the actual Python parser? I don't know
about the former, but the syntax is no problem for the latter.
> The end of a class definition might be slightly more complex to check
> that all "pure" methods have implementations, but (almost certianly)
> not hopelessly so... and you get to detect the "not implemented" error
> early with a "missing definition" error rather then later with a
> "can't instantiate" error.
You need to re-read PEP 245; no such compile time checking or error
conditions are proposed, and I think that requireing that checking is a
bad idea (in addition to being impossible for anything but the most naive
cases, since you can change a class at run time).
These are not like Java interfaces. Interface enforcement can certainly
be done optionally, but it is not a requirment and it never should be
because a) it is expensive, b) the object should be implicitly trusted
unless you feel otherwise (and then you use optional enforcement) and c)
the problem is unbounded: at first you want to check just attributes, and
then parameters, and then assertions, and then types, whoa! Everybody's
requirement is different (go read the types-sig and you'll see what I
mean), by making all of these enforcements optional, everybody gets to be
happy with their own.
Java and other static languages get away with this because all interfaces
do is assert compile time enforcement, this would not work for Python
which is much more dynamic.
> Surely a python interpreter could be smart enough to realize that some
> name in the list of inherited objects is actually an interface?
The interpreter? Surely. The guy reading your code? No way.
Thanks for you comments, if you'd like, I can add your suggestion to the
PEP under the different opinions section.
-Michel
More information about the Python-list
mailing list