Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
Forked from jasonLaster/dd.py
Created July 3, 2012 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasballinger/3041354 to your computer and use it in GitHub Desktop.
Save thomasballinger/3041354 to your computer and use it in GitHub Desktop.
Dynamic Dispatch in Python
class FlexiDict(dict):
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