[Python-Dev] Sneaky 'super' instances

Phillip J. Eby pje@telecommunity.com
Thu, 12 Jun 2003 12:11:37 -0400


At 05:35 PM 6/12/03 +0200, Samuele Pedroni wrote:
>Once I already sketched in a mail to the list a possible solution to this 
>problem. I have read a bit PyProtocols documentation and I think that my 
>idea is probably implementatble on top of PyProtocols.
>
>The idea is to allow for anonymous subsetting of protocols:
>
>subproto(IFile,'read'[,...])
>
>there would be a cache to guarantee uniqueness of these things.
>Given that in
>
>subproto(Protocol, method-names)
>
>len(method-names) would be small (<5), even if there exponatial many 
>subsets of method-names :) it should be tractable to automatically 
>register all the natural relationships:
>subproto(IFile,'read','write') > subproto(IFile,'read')
>
>Obviously these anonymous sub-protocols would be instantiated on demand, 
>not when IFile is created.

Hmmm...  this is *very* interesting.  You're right, it *could* be 
implemented on top of the existing PyProtocols.


>The final point then is that there should be a [global?] flag to choose 
>between strict or loose checking for this protocols:
>
>ReadProto=subproto(IFile,'read')
>
>aobj = adapt(obj,ReadProto)
>
>in loose mode obj would directly adapt to ReadProto if it has a 'read' 
>attribute.
>In strict mode some adaption path should have been registered explicitly: 
>e.g.:
>
>class MyObj:
>
>   def read():
>     ...
>
>declareImplementation(MyObj,[subproto(IFile,'read')])

I think this could be done by giving the 'subproto' a "loose" version.  The 
strict version would imply the loose version, but not vice versa.

There are some mechanics to work out for how the cache would work (from a 
reference retaining perspective).  But this seems like a nice sample to add 
to the docs: a 'LooseSubset' Protocol subclass whose __adapt__ can do this 
kind of checking.  Or maybe I should add a complete implementation to the 
next release.  I've been similarly thinking about adding other demo 
protocol types like parameterized protocols (e.g. 
'SequenceOf(otherProtocol)' or 'MappingOf(keys=keyKind,values=valueKind)').

Thank you very much for the idea!