I'm writing an "envirorment" where each variable is composed by a value and a description:
class my_var:
def __init__(self, value, description):
self.value = value
self.description = description
Variables are created and put inside a dictionary:
my_dict["foo"] = my_var(0.5, "A foo var")
This is cool but 99% of operations with variable are with the "value" member. So I have to write like this:
print my_dict["foo"].value + 15 # Prints 15.5
or
my_dict["foo"].value = 17
I'd like that all operation on the object my_dict["foo"] could default to the "value" member. In other words I'd like to write:
print my_dict["foo"] + 15 # Prints 5.5
and stuff like that.
The only way I found is to reimplement all underscore-members (eq, add, str, etc) but I feel like this is the wrong way somehow. Is there a magic method I could use?
A workaround would be to have more dictionaries, like this:
my_dict_value["foo"] = 0.5
my_dict_description["foo"] = "A foo var"
but I don't like this solution. Do you have any suggestions?