I have a class with many attributes, and when I give a number, I would like to subtract that from a feature , But if the amount is less than the attributed feature, then proceed to the next attribute which is the remainder Example:
Diff sub-tabs (self, amount): self.attr1 + = The amount if self.attr1 & lt; 0: self.attr2 + = self.attr1 self.attr1 = 0 if self.attr2 & lt; 0: # etc ... It seems that there should be a brief recursive method to complete the same thing, but I do not know how to do with all the different attributes. .decc
You use attributes using .__ dict __ can do. Want to subtract something works like this. class A (): def __init __ (self): self.foo = 100 self.bar = 200 self.baz = 300 self.sub_stack = ['foo', 'baz', 'Bar'] def subt (self, zodiac): tmp_stack = self.sub_stack [:] while tmp_stack and zodiac: key = tmp_stack.pop (0) val = self .__ dict __ [key] if val & gt; Zodiac: Self .__ word __ [key] - = zodiac = 0 and: zodiac - = self .__ word __ [key] own .__ word __ [key] = 0 return amount < P> Return value is the balance after the amount through your attributes
Comments
Post a Comment