[Python-Dev] PEP 443 - Single-dispatch generic functions

Armin Rigo arigo at tunes.org
Thu May 23 09:33:33 CEST 2013


On Thu, May 23, 2013 at 12:33 AM, Łukasz Langa <lukasz at langa.pl> wrote:
> Alternative approaches
> ======================

You could also mention "pairtype", used in PyPy:
(very short code).  It's originally about adding double-dispatch, but
the usage that grew out of it is for generic single-dispatch functions
that are bound to some common "state" object as follows (Python 2

class MyRepr(object):
     ...state of my repr...

class __extend__(pairtype(MyRepr, int)):
    def show((myrepr, x), y):
         print "hi, I'm the integer %d, arg is %s" % (x, y)

class __extend__(pairtype(MyRepr, list)):
    def show((myrepr, x), y):
         print "hi, I'm a list"
         ...use myrepr to control the state...

pair(MyRepr(), [2,3,4]).show(42)

- Armin

More information about the Python-Dev mailing list