Created
October 24, 2012 16:52
-
-
Save thomasballinger/3947297 to your computer and use it in GitHub Desktop.
dynamic att lookup in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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