[PEAK] Type and class adaptation
Phillip J. Eby
pje at telecommunity.com
Fri Nov 19 14:15:30 EST 2004
On my second reading, I caught a couple of things that I missed before...
At 06:21 PM 11/19/04 +0100, Radek Kanovsky wrote:
>Additionally if I declare
>adapter for MyClass, this adapter is not applicable to SubClass.
This doesn't sound right; do you have a short code sample I could use to
>def dispatch_by_subclass (klass, table):
> while True:
> if klass in table:
> return table[klass]
> klass, = klass.__bases__
This code will raise an AttributeError if it is used on a non-class.
> except ValueError:
> if klass.__bases__:
> return table.reseed(klass)
> if klass in table:
> return table[object]
This last bit is also broken; it's *always* going to return None.
Essentially, the problem with your implementation as far as I can tell, is
that it doesn't at all consider the possibility that the object is not a
class. I would recommend that you use 'None' as the key for that
situation. So, the code would look something like this:
def dispatch_by_subclass (klass, table):
if klass in table:
klass, = klass.__bases__
if None in table:
And, the 'ISSubclass.seeds()' method should also include 'None' in its seeds.
Essentially, a dispatch function must never return 'None' unless there are
no cases registered for that situation, and an ITest must create seeds for
all the possibilities regarding that test. One of the possibilities of a
subclass test is that the object being tested is not a class, so there
needs to be a key for that condition. Also, there is the possibility that
a class is not a subclass of any known test (i.e., it's a new classic
class). In both of these cases, the 'None' key represents the default
dispatch path, which is basically a path where none of the subclass-testing
alternatives are applicable.
This is different from dispatch_by_mro, because in Python everything is an
*instance* of 'object' eventually, so 'InstanceType' and 'object' are the
default keys. But, in this case, not everything is a *subclass* of
'object', so a different default is needed.
More information about the PEAK