I have a class with many attributes, and when I give a number, I would like it to subtract that from one attribute, but if the amount is greater than the attribute subtracted from, move to the next attribute with what is left over. Example:
def subt(self, amount):
self.attr1 += amount
if self.attr1 < 0:
self.attr2 += self.attr1
self.attr1 = 0
if self.attr2 < 0:
# etc...
It feel like there should be a concise recursive way to accomplish the same thing, but I don’t know how with the all the different attributes.
You can access the attributes using
.__dict__You need a list for the order you want to subtract. Something like this works.return value is the remainder on amount after iterating through your attributes