[GvR] > I think you can probably get something that is reasonable given the > kind of assumptions that prevail in an ABC world by having a > "Sortable" metaclass (itself a subclass of ABCMeta) with the implied > semantics that objects x and y can be sorted if the nearest common > ancestor of x.__class__ and y.__class__ is an instance of Sortable. > E.g. > > class S(metaclass=Sortable): ... > > class S0(S): > def __lt__(self, other): ... > def __le__(self, other): ... > ...etc... > > class S1(S0): ... > > class S2(S0): ... > > x = S1() > y = S2() > > Now x<y should work: the nearest common ancestor of S1 and S2 is S0, > which is a subclass of S, which is an instance of Sortable. (If S is > an instance of Sortable, so is S0 -- that's how metaclasses are > inherited.) > > But I'm not sure this is worth it; we don't have an easy way to add > this to an argument annotation, since the argument annotations are > (usually) assumed to be type expressions so that f(x:T) means > isinstance(x,T) -- but here we want to express isinstance(x.__class__, > Sortable). The best I can think of is to say that if the annotation is > a lambda (not just any callable, since classes are callable) it must > evaluate to true when called on the actual argument: then we'd write > f(x: (lambda x: isinstance(x.__class__, Sortable))). Very ugly, but > could of course be abbreviated with a suitable helper function to f(x: > metaclass_is(Sortable)).
Wow! That's a lot of firepower for such a little problem ;-) Raymond _______________________________________________ Python-3000 mailing list [email protected] http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com
