ANSWER AT THE END
I am trying to monkey patch a class with attributes that are assigned programatically, but I am running into a variable scope problem that I don't know how to solve. See the code below:
class my_obj_class:
pass
my_obj = my_obj_class()
letters = ['a', 'b']
numbers = [1, 2]
for l in letters:
for n in numbers:
attribute_name = "print_" + l + str(n)
def print_ln(self):
print(l, n)
setattr(my_obj_class, attribute_name, print_ln)
my_obj.print_a1()
l, n = "c", 3
my_obj.print_a1()
prints:
b 2
c 3
Obviously this isn't working the right way (print_a1 always prints "a1") because the print_ln is printing the variables l and n which are changing in the for loop and each attribute is using the same two variables. I can't seem to conceive how to do this so that print_a1 always prints "a1" and not a value of a variable other than to hard code each attribute, which I am obviously trying not to do.
ANSWER
Yes, thanks for the comments, the answer is closures, which are new to me. The for working loop is below:
for l in letters:
for n in numbers:
attribute_name = "print_" + l + str(n)
def print_ln(l, n):
def printer(self):
print(l, n)
return printer
setattr(my_obj_class, attribute_name, print_ln(l, n))