This is a simple demo about what I said:
class Holder(object):
def __init__(self, init_dict):
self.dict = init_dict
def increase(self, key):
if key in self.dict:
d = {key: self.dict[key] + 1}
self.update(d)
def update(self, d):
self.dict.update(d)
self.print_prompt()
def print_prompt(self):
for key in sorted(self.dict.keys()):
print '%s: %d' % (key, self.dict[key])
print
def main():
sounds = {
'sound1.mp3': 0,
'sound2.mp3': 0,
'sound3.mp3': 0
}
holder = Holder(sounds)
holder.print_prompt()
holder.increase('sound1.mp3')
holder.update({
'sound1.mp3': 3,
'sound2.mp3': 2,
'sound3.mp3': 1
})
if __name__ == '__main__':
main()
The output is:
sound1.mp3: 0
sound2.mp3: 0
sound3.mp3: 0
sound1.mp3: 1
sound2.mp3: 0
sound3.mp3: 0
sound1.mp3: 3
sound2.mp3: 2
sound3.mp3: 1