Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
Created October 24, 2012 16:52
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/3947297 to your computer and use it in GitHub Desktop.
Save thomasballinger/3947297 to your computer and use it in GitHub Desktop.
dynamic att lookup in Python
class Other(object):
def Task(self, x):
print 'called task'
class Foo(object):
def __init__(self):
self.tom = 12
self.stuff = []
@property
def foo(self):
return 'adfs'
def __getattr__(self, key):
allowed_objects = [Other, ]
names = {x.__name__: x for x in allowed_objects}
def new_func(*args, **kwargs):
x = names[key](*args, **kwargs)
self.stuff.append(x)
return x
return new_func
f = Foo()
from bpython import cli; cli.main(locals_=locals())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment