[Python-Dev] PEP 246, redux
Phillip J. Eby
pje at telecommunity.com
Tue Jan 11 19:03:18 CET 2005
At 10:59 AM 1/11/05 +0100, Alex Martelli wrote:
>all you have to do is ADD
> def __conform__(self, protocol):
> if issubclass(protocol, Abstract):
> raise LiskovViolation
>
>that's all.
That will raise a TypeError if protocol is not a class or type, so this
could probably serve as an example of how difficult it is to write a good
Liskov-violating __conform__. :)
Actually, there's another problem with it; if you do this:
class Y(X): pass
class Z(Y): pass
then 'adapt(Z(),Y)' will now fail because of a Liskov violation. It should
really check for 'protocol is Abstract' or 'protocol in (Abstract,..)' in
order to avoid this issue.
>Basically, rephrasing private inheritance with containment and delegation
>is a lot of messy work, and results in far more complicated
>structures. And instead of paying the tiny price of a __conform__ call at
>adaptation time, you pay the price of delegating calls over and over at
>each x.tm1() call, so it's unlikely performance will improve.
Well, as I mentioned in my other post, such inheritance is a lot simpler
with PEAK, so I've probably forgotten how hard it is if you're not using
PEAK. :) PEAK also caches the delegated methods in the instance's
__dict__, so there's virtually no performance penalty after the first access.
Again, not an argument that others should use PEAK, just an explanation as
to why I missed this point; I've been using PEAK's delegation features for
quite some time and so tend to think of delegation as something relatively
trivial.
More information about the Python-Dev
mailing list