[Python-3000] Breakthrough in thinking about ABCs (PEPs 3119 and 3141) (original) (raw)
Joel Bender jjb5 at cornell.edu
Tue May 1 18:16:08 CEST 2007
- Previous message: [Python-3000] Breakthrough in thinking about ABCs (PEPs 3119 and 3141)
- Next message: [Python-3000] Breakthrough in thinking about ABCs (PEPs 3119 and 3141)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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...
class X:
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)...
class Y:
@classmethod
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".
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().
Joel
- Previous message: [Python-3000] Breakthrough in thinking about ABCs (PEPs 3119 and 3141)
- Next message: [Python-3000] Breakthrough in thinking about ABCs (PEPs 3119 and 3141)
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]