[PEAK] protocols question

Phillip J. Eby pje at telecommunity.com
Mon Mar 29 09:44:16 EST 2004


At 09:37 PM 3/28/04 +0200, Gabriel Jägenstedt wrote:
>There is a thing that has been bothering me with pyprotocols.
>I have been trying to use adapt to get an instance of a component as I
>belive is the meaning of it.

Adapting takes an input object, and returns the object if it provides the 
desired protocol.  If the input object does *not* provide the protocol, a 
lookup takes place to find an adapter function that will convert the object 
or create a wrapper to adapt it.



>I asume there is something missing from my code but I haven't been able
>to find any reference to how it all should be set up.
>
>Anyway as it's only a test the code is quite meaningless.
>
>class IEdible(Interface):
>     """Implements an edible behaviour on objects"""
>
>     def __init__():
>         """a number of attributes that should be enabled in child"""
>
>
>     def PreObj(self, cmd=''):
>         """returns a cmd'string'"""
>
>
>class Food:
>     advise(instancesProvide = [IEdible])
>
>     def __init__(self, pNames=''):
>         self.id = 12
>
>     def PreObj(self, cmd=''):
>         print 'hurray'
>
>adapt(IEdible, Food)

Food is a class.  It does not provide IEdible, and you have not defined any 
adapters from it to IEdible.

However, you *have* declared that *instances* of Food provide IEdible.  So 
this:

adapt(IEdible,Food())

will return you the Food instance you passed in.




More information about the PEAK mailing list