Yet Another PEP: Query Protocol Interface or __query__
Huaiyu Zhu
hzhu at mars.localdomain
Wed Mar 21 03:41:17 EST 2001
On Tue, 20 Mar 2001 20:49:30 -0500, Clark C. Evans <cce at clarkevans.com> wrote:
>On Wed, 21 Mar 2001, Alex Martelli wrote:
>>
>> At least when read at 1 AM -- sounds cool. Further, the
>> builtin query function could support _adding interfaces to
>> an object post-facto, non-invasively_, much like the
>> 'typeclass'-based ideas that were floating in my mind
>> recently and surfaced in some of my recent posts.
>
>Right.
>
>> I.e., the query() builtin would both try getting the interface
>> from the _object_, by passing that interface to the obj's
>> query method, AND also try doing it the other way
>> 'round -- asking the interface if it knows how to wrap
>> the object so it (the interface) gets implemented.
>
>That is a very cool idea! And the reverse lookup could
>even be added much later as a seperate PEP. I was trying
>to stay simple and to de-couple this PEP as much as possible
>from the other possibilities being tossed about in the
>type special interest group. I'd hate to have a dependency
>emerge when one really isn't necessary.
I have an implementation of this idea at
http://www.geocities.com/huaiyu_zhu/python/Interface/typecheck.txt
(You can try it. This is in fact a .py file, but geocities does not allow
that extension. Should get a Starship account someday.)
The explantions are here
http://www.geocities.com/huaiyu_zhu/python/Interface/README.txt
Summary of points:
1. It should be possible to define interface after the fact, without
redefining the objects.
2. It should be possible to use it for builtin types and other things, not
just class objects. Eg., it allows saying a particular instance of a
class satisfies the interface without dragging all other instances of
this class in.
3. It should allow "reversed inheritance". That is, saying things in this
order would allow f(sys.stdin) to work:
sys.stdin is a file
file is a kind of stream
f want a stream as argument
In other words, you can build up interfaces by going from specific to
general, in contrast to class inheritance which goes the other way.
If you are interested, there are a few other files at
http://www.geocities.com/huaiyu_zhu/python/Interface/
--
Huaiyu Zhu <hzhu at users.sourceforge.net>
More information about the Python-list
mailing list