Skip to content
Snippets Groups Projects
Commit 616bf544 authored by Dominic Kempf's avatar Dominic Kempf
Browse files

Add a delegating multifunction pattern

parent 183fd02c
No related branches found
No related tags found
No related merge requests found
""" Implement a design pattern for a multifunction that delegates to another
multifunction and gets back control if the delegate does not define a handler.
This avoids writing isinstance-if-orgies in handlers
"""
def delegate(Delegate, *args, **kwargs):
assert(isinstance(Delegate, type))
assert(Delegate.expr == Delegate.undefined)
class MyDelegate(Delegate):
def __init__(self, *a, **ka):
Delegate.__init__(self, *a, **ka)
def expr(s, *a, **ka):
s._back(*a, **ka)
def _handler(s, *a, **ka):
delegate_instance = MyDelegate(*args, **kwargs)
delegate_instance._back = s
return delegate_instance(*a, **ka)
return _handler
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment