So, I run into this problem ALL the time with my dictionary pairs. Here's the simple version:
Whenever I create a dictionary, I don't like to initialize values that I know I won't need. Presently, the generation step is as such:
raw_list = ['$P','CP','PYR','AQA','GEA','AEV','HOL','JNX','PUR','MGC']
for item in raw_list:
self[item] = 0
Often, I only need at most like four of those, although certain exceptions apply.
The work-around that I'm using will probably resemble the one I'm using for yet another dictionary-based class in the program:
if it in self:
self[it] += amt
self[it] = amt
Which auto-initializes values that are not yet in the dictionary. It would be much clearer for me if I could, instead of using this method as-written (comp_node.compAdd(it,amt)), have it function as the default += operator for the comp_node class.
So my questions are as follows:
Is it possible to build custom arithmetic-sign methods ( as explained above) for dictionary objects?
If possible, how can I design new methods for the built-in classes? I really would like such a += operator for all of my dictionaries.
To specify, I'd imagine it behaving as:
comp_node[it] += amt