I'm trying to create properties at runtime for a class, and i'm currently using this piece of code:
class Obj:
pass
attrs = ["a", "b", "c"]
for attr in attrs:
attr_print = lambda self: print(attr)
setattr(Obj, attr, property(attr_print))
o = Obj()
o.a
But whatever attribute i'm trying to print, it always prints the last in attrs
.
It feels like python is changing my previous lambdas at each loop.
Is there a way to prevent this happening ?