[Python-Dev] PEP 246, redux
Nick Coghlan
ncoghlan at iinet.net.au
Tue Jan 11 09:18:24 EST 2005
Alex Martelli wrote:
> "I really wish the language had
> private inheritance because I'm using Abstract as a base just for code
> reuse"
Funny you should say that. . . what about a __nonconformant__ entry that accepts
a list of base classes that is used to indicate inheritance without a proper
is-a relationship?
So if the isinstance check succeeds and there is no '__nonconformant__' entry,
then adapt() just returns the object.
If, on the other hand, __nonconformant__ is supplied, then adapt() can check the
list of base classes that remains after removing the entries in
__nonconformant__ to see if the object would *still* be an instance a subtype,
even after removing the noncomformant bases, and if it is, return it.
Otherwise, continue on to the rest of the adaptation process.
This should give a better idea what I mean:
# The 'fast path'
if isinstance(obj, protocol):
if not hasattr(obj, "__nonconformant__"):
return obj
conformant_bases = set(obj.__bases__) - set(obj.__nonconformant__)
for base in conformant_bases:
if issubtype(base, protocol):
return obj
# Continue on with the other adaptation possibilities (including __conform__)
Then you can get 'discreet' inheritance (you have the methods, but you don't
brag about the fact) by writing:
class Dubious(Abstract):
__noncomformant__ = [Abstract]
# etc
rather than:
class Dubious(Abstract):
def __comform__(self, protocol):
if issubtype(protocol, Abstract):
raise LiskovViolation
# etc
Regards,
Nick.
--
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
More information about the Python-list
mailing list