[TransWarp] PyProtocols: adapting classes to instance

Ian Bicking ianb at colorstudy.com
Sun Sep 28 15:54:16 EDT 2003

I wasn't sure if this is the right place to post questions about 

Anyway, I want to adapt classes to instances of those classes.  So you 
could do something like:

class MyValidator(Validator):

adapt(MyValidator, IValidator)
# --> MyValidator()

This way classes can be used in place of instances in most cases (and 
obviously the classes are written with this in mind).  But I haven't 
been sure how to do this.

I tried using a metaclass like:

class ValidatorMeta(type):
     def __new__(meta, className, bases, d):
         cls = type.__new__(meta, className, bases, d)
         protocols.declareAdapter(_simpleClassFactory, cls.__provides__,
         return cls

def _simpleClassFactory(v, protocol): return v()

(I set cls.__provides__ manually because I wasn't sure how to find what 
protocols an object supports from PyProtocols, but that's an aside)

Unfortunately that didn't work.  I got an error 'Incompatible 
__conform__ on adapted object'.  I assume that's because Validator 
defines a __conform__ method, which applies to instances (for 
conforming validators to a different interface).

So, my basic question (if you understand what I'm trying to do) -- how 
should I do it?  I might be all backwards, so I'm open to any ideas.  
Add an __adapt__ method to IValidator?  Add a __conform__ method to 
ValidatorMeta?  I've tried different combinations, but to no avail.


More information about the PEAK mailing list