I have a class which has multiple methods defined.
import mat
class Klass(object):
@mat.sell(mat.CanSet):
def method1(self):
return None
@mat.sell(mat.CanSet):
def method2(self):
return 'value2'
Imagine I have 10 methods that I need to populate for this 'Klass'. I want to generate these methods without explicitely writing them all. So I want to do a factory that does setattr for each method. Problem is that I do following and the last method has the last value. Each do not get its related value but value10. Also below solution does not implement the decorator, I have no idea how to do assign the decorator
class Klass(object):
pass
list1 = [('method1', 'value1'), ('method2', 'value2').....('method10', 'value10')]
for each in list1:
method_name, method_value = each
setattr(Klass, method_name, lambda self: method_value)
So when I do following....
k = Klass()
print k.method1(), method2()...., method10()
it all results in value10 for each method. Do not understand why ? Plus, can anyone help on how to implement the decorator with one attribute ? PS: if you have suggestion that does not use 'setattr', that would be welcomed as well.