[Types-sig] QueryProtocol
Michel Pelletier
michel@digicool.com
Sat, 24 Mar 2001 21:07:36 -0800 (PST)
On Sat, 24 Mar 2001, Clark C. Evans wrote:
> Further, I still do not have a PEP number. Either I have
> not asked correctly, am being stupid, have pissed someone
> off, or am proposing something that has already been
> rejected. In any case, what do I do?
I suspect of the four possibilites, the first is the most likely. Did you
read PEP 1?
> PEP: XXX
> Title: Protocol Checking and Adaptation
> Version: $Revision$
> Author: Clark Evans
> Python-Version: 2.2
> Status: Draft
> Type: Standards Track
> Created: 21-Mar-2001
> Updated: 23-Mar-2001
>
> Relationship to Python Interfaces [1] by Michel Pelletier
>
> The relationship to this proposal to Michels proposal could
> also be complementary. If approved, the first two paragraphs
> of the built-in adapt function could be changed as follows:
>
> # first check to see if object has the exact protocol
> > if type(protocol) is types.InterfaceType and \
> > instance(obj,protocol): return obj
> if type(obj) is types.InstanceType and \
> obj.__class__ is protocol: return obj
> if type(obj) is protocol: return obj
>
> # procedure to execute on success
> def succeed(retval,obj,protocol,can_wrap):
> > if type(protocol) is types.InterfaceType:
> > if not instance(retval,protocol):
> > raise "Bad __adapt__ or __prodapt__!"
> if can_wrap: return retval
> return obj
Actually, you would say:
if implements(obj, protocol): return obj
An object that implements an interface is not an instance of that
interface, so 'instance' would fail (did you mean 'isinstance'?).
-Michel