[Python-3000] Generic functions

Ian Bicking ianb at colorstudy.com
Tue Apr 4 17:38:16 CEST 2006


Tim Hochberg wrote:
> Rewriting this using "distributed" adapters looks like this:
> 
>  >>> pprint = Protocol('pprint')

I suppose this means that Protocol('pprint') implies an interface like:

class IPprint(Interface):
     def __call__(object):
         """print the pretty representation of the object"""


Viewed from this perspective, RuleDispatch-style generic functions use 
the default implementation as the description of the interface, and only 
allow for callable interfaces.

>  >>> @pprint.when(object)
> ... def pprint_obj(obj):
> ...     print repr(obj)
> 
>  >>> @pprint.when(list)
> ... def pprint_list(obj):
> ...     print 'pretty(%r)' % obj
> 
> 
> Pretty similar! Stealing the when decorator cleans up the use of 
> copy_reg.pickle rewrite a bit as well. I imagine that where these would 
> diverge is when there is more than one argument.
> 
> 
> Regards,
> 
> -tim
> 
> 
> P.S., Here's Protocol. To save a few bytes I've stripped classic class 
> support.

I suppose on the py3k list we don't have to apologize for ignoring 
classic classes ;)


-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the Python-3000 mailing list