3

If I have Bag(a=1,b=2,c=3) and I want to change the individual values. I can call self.a = 10 so my updated output would be: Bag(a=10,b=2,c=3).

If k = a and I have the code:

temp = ("self." + str(k))
print(temp) --> self.a

How can I do something like:

temp = 10

So it updates the bag (not temp itself to 10)?

Thanks

MTG
  • 163
  • 2
  • 12
  • There is no `Bag` class in the Python standard library. How is `Bag` defined in your code? Do you mean a `dict`? – phihag Feb 17 '18 at 03:34

1 Answers1

3

Sounds like you're looking for setattr:

setattr(self, k, 10)
wim
  • 338,267
  • 99
  • 616
  • 750