Yet Another PEP: Query Protocol Interface or __query__

Clark C. Evans cce at clarkevans.com
Tue Mar 20 21:22:45 EST 2001


On 20 Mar 2001, Aahz Maruch wrote:
> Clark C. Evans <cce at clarkevans.com> wrote:
> >
> >    A new built-in method, __query__ is proposed.  This method has
> >    a single argument, an interface protocol identifier, and either
> >    returns an object supporting the given protocol, or throws an
> >    unknown exception.
> >
> >    An interface protocol identifier is a lower-case string having
> >    a reverse DNS host name string:   TLD '.' DOMAIN ['.' NAME ]*
> >    Where TLD is a top level domain such as 'com' or 'uk.co'.  DOMAIN
> >    is a registered name in the given TLD.  And one or more optional
> >    NAME separated by a period.  NAME is a sequence of one or more
> >    alphabetic characters including the dash '-' character.  See the
> >    relevant ITEF specifications for specific details.
> 
> It's not at all clear to me why this domain thing is being suggested.

A unique identifier is needed.  One could use GUIDS, but I 
think that this would be less readable and maintainable.

> Is there supposed to be some kind of lookup mechanism 
> associated with it?

Yes.  I was planning that 

  query(obj,"org.python.example.eggs")

would behind the scenes call __query__ on the 
object obj.  This indirection allows for 
additional "hooks" to be added to the system 
via another PEP that could perform a wrapping
function.  See Alex's recent post.

> Most importantly, how does one find out what interfaces 
> an object *does* support?

I don't have this need -- so I didn't include it.  I suppose
we could ammend the proposal to have this functionality.
Perhaps obj.__query__() could return a sequence (or better 
yet a new iterator object).

What do you think?

> >Example Usage
> >
> >    >>> class EggsOnly:
> >            def eggs(self,str): print "eggs!" + str
> >            def __query__(self,protocol):
> >                if protocol == "org.python.example.eggs": return self
> >                raise "unknown: " + protocol
> >
> >    >>> class EggsAndHam:
> >            def ham(self,str): print "ham!"+str
> >            def __query__(self,protocol):
> >                if protocol == "org.python.example.ham": return self
> >                if protocol == "org.python.example.eggs":
> >                    return EggsOnly()
> >                raise "unknown" + protocol
> 
> This seems to be implying that it is appropriate to return a brand-new
> object with no association with the original object.  Is that correct?

Yes.  The __query__ mechansim is vague in this respect,
no use to have any artificial limitations.

> 
> Does it make sense to have something like:
> 
> class HamAndEggs:
>     def __query__(self, protocol):
>         if protocol.find("org.python.example.ham") == 0:
>             return self
> h = HamAndEggs()
> e = h.__query__("org.python.example.ham.eggs")


Let me see, a string goes in, and an object 
comes out. Sure.  Looks good to me!

Clark






More information about the Python-list mailing list