The PEAK Developers' Center   Diff for "ProtocolSubsets" UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View
Ignore changes in the amount of whitespace

Differences between version dated 2004-07-13 23:59:39 and 2004-07-14 17:01:30

Deletions are marked like this.
Additions are marked like this.

I originally posted this [http://goldenspud.com/webrog/archives/2004/07/13/pyprotocols-interface-subclasses-vs-interface-subsets/ here] but figured it would be good netiquette to re-post the details of it on this wiki :-) Comments/corrections greatly appreciated.
 
If you have code similar to the following, which produces a "TypeError" with an "Ambiguous adapter choice" error message, you are probably making an incorrect assumption about how to differentiate interfaces that are related to eachother via another interface that is common between them. ("Fixed" code is below the "Broken" code)
If you have code similar to the following, which produces a "TypeError" with an "Ambiguous adapter choice" error message, you are missing an adapter that makes the choice unambiguous:
 
{{{
#!/usr/bin/env python

z = IZee(x); print 'z', z.value
}}}
 
Subclassing IBase as shown above *extends* it which make IWhy and IZee ambiguously similar since they both have the entire IBase interface in common. Instead, to differentiate them you want to make IWhy and IZee *subsets* of IBase:
By adding this adapter *above* the other two adapters, you make the adapter choices unambiguous: (In this case, we do not want to allow adapting type X to interface IBase)
 
{{{
protocols.declareAdapter(
    factory=protocols.DOES_NOT_SUPPORT,
    provides=[IBase],
    forTypes[X],
    )
}}}
 
What you '''DO NOT WANT TO DO''' is to make subsets of the base interface. Even though this makes adapter choices unambiguous, it still has the opposite effect of what you might have intended. Do not use the following pattern to fix the problem described above. See Phillip Eby's comments on the original post linked to above for a more thorough explanation.
 
{{{
#!/usr/bin/env python

PythonPowered
ShowText of this page
EditText of this page
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck