The PEAK Developers' Center   WritingAdaptors UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View

1 Writing Adaptors

Taken from the mail list thread http://www.eby-sarna.com/pipermail/peak/2004-March/001356.html:

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.

From a user example:

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.


PythonPowered
EditText of this page (last modified 2005-07-29 17:25:06)
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck