Hi,

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

You could also mention "pairtype", used in PyPy:
https://bitbucket.org/pypy/pypy/raw/default/rpython/tool/pairtype.py
(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
syntax):

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
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to