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

1
  • There is no Bag class in the Python standard library. How is Bag defined in your code? Do you mean a dict? Commented Feb 17, 2018 at 3:34

1 Answer 1

3

Sounds like you're looking for setattr:

setattr(self, k, 10)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.