[Python-3000] Breakthrough in thinking about ABCs (PEPs 3119 and 3141)
jjb5 at cornell.edu
Tue May 1 18:16:08 CEST 2007
Phillip J. Eby wrote:
>> Personally, I still think that the most uniform way of spelling this
>> is overloading isinstance and issubclass; that has the highest
>> likelihood of standardizing the spelling for such inquiries.
> A big +1 here. This is no different than e.g. operator.mul() being able to
> do different things depending on the second argument.
n00b here, trying to follow this...
def __mul__(self, y): print self, "mul", y
def __rmul__(self, y): print self, "rmul", y
Treating isinstance like operator.mul, I could do this (and I would
expect that you want to make it a class method)...
def __risinstance__(cls, obj): print obj, "is instance of", cls
So issubclass(D, C) would call D.__issubclass__(C) or
C.__rissubclass__(D) and leave it up to the programmer. 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".
def __rissubclass__(cls, subcls):
if not object.__rissubclass__(cls, subcls):
return subcls.f is not A.f
raise RuntimeError, "f must be overridden"
def g(self): print "B.g"
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().
More information about the Python-3000