[Python-3000] Breakthrough in thinking about ABCs (PEPs 3119 and 3141)
Phillip J. Eby
pje at telecommunity.com
Tue May 1 18:44:49 CEST 2007
At 12:16 PM 5/1/2007 -0400, Joel Bender wrote:
>So issubclass(D, C) would call D.__issubclass__(C) or
>C.__rissubclass__(D) and leave it up to the programmer.
Yes, except there's only the '__r__' versions and they're not called that.
> The former is
>"somebody is checking to see if I inherit some functionality" and the
>latter is "somebody is checking to see if something is a proper derived
>class of me".
>
> class A(object):
> @classmethod
> def __rissubclass__(cls, subcls):
> if not object.__rissubclass__(cls, subcls):
> return False
> return subcls.f is not A.f
>
> def f(self):
> raise RuntimeError, "f must be overridden"
>
> class B(A):
> def g(self): print "B.g"
>
> class C(A):
> def f(self): print "C.f"
>
>Now my testing can check issubclass(B, A) and it will fail because B.f
>hasn't been provided, but issubclass(C, A) passes. I don't have to call
>B().f() and have it fail, it might be expensive to create a B().
Right; you've just pointed out something important that hasn't been stated
until now. The objections to this extension have focused on the idea that
this makes type checking less strict, but you've just demonstrated that it
can actually be used to make it *more* strict. I hadn't thought of that.
More information about the Python-3000
mailing list