I have the following class:
class Resources:
"""
Resources class.
"""
def __init__(self, food: int, gold: int):
self.F = food
self.G = gold
def __sub__(self, other):
return Resources(self.F - other.F, self.G - other.G)
def __getitem__(self, attr):
return self.__getattribute__(attr)
I am coding another class for buying / selling specific resources. The part Im struggling with is this:
a = Resources(100, 200)
a['F'] -= 50
When executed, TypeError: 'Resources' object does not support item assignment
I can get any attribute once I know its name, but I don't know how to change its value through the -= operator.
So, to clarify, the question is: How to substract a float value to a given attribute selected by using a string, and store the result in the same attribute?