How can I do something like this:
class Foo():
do_stuff = {
"A" : lambda x: self.do_A(x),
"B" : lambda x: self.do_B(x)
}
def __init__(self):
print "hi"
def run(self):
muh = ['A', 'B', 'A']
for each in muh:
self.do_stuff[each](each)
def do_A(self, moo):
print "A"
def do_B(self, boo):
print "B"
if(__name__ == '__main__'):
aFoo = Foo()
aFoo.run()
This leads to the fact that it gives an error that self is not defined in the lambda function, but if I delete it. He says do_A or do_B are not defined.
EDIT
I managed to figure it out. I need to change the lambda expression to something like this:
lambda x, y: x.do_A(y)
and I would call it that:
self.do_stuff[each](self, each)
Is this a terrible idea?
source
share