Skip to content

Instantly share code, notes, and snippets.

@jasonLaster
Created July 3, 2012 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jasonLaster/3040877 to your computer and use it in GitHub Desktop.
Save jasonLaster/3040877 to your computer and use it in GitHub Desktop.
Dynamic Dispatch in Python
class FlexiDict(dict, DDispatch):
def get_gt(self, key):
return [v for k,v in d.iteritems() if k > key]
def __getattr__(self, name):
if name.startswith("find_"):
params = name.split("_")
if len(params) == 3:
def func():
return dict.__getattribute__(self, "get_gt")(int(params[2]))
return func
else:
def func():
return dict.__getattribute__(self, "get")(int(params[1]))
return func
d = FlexiDict(list(enumerate('abcde')))
print d.find_1()
print d.find_gt_2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment