Skip to content Skip to sidebar Skip to footer

Increment An Integer By Calling A Hashtable Value In Python 3?

You'd think it'd be easy. I have the following code: key_handlers=collections.defaultdict(lambda: None) key_handlers['+']=operator.iadd(tickrate,1) key_handlers['-']=operator.iadd(

Solution 1:

You can stage the defaultdict with a default value of your choice. For instance:

from collections import defaultdict
key_handlers = defaultdict(lambda : tickrate)

But, from the example above, what I think you really want is a dict of methods that update either global state. This is expanding from the comment from @utdemir above:

global tickrate
key_kandlers = {'+': lambda : 1, '-': lambda : -1}
func = key_handlers.get(event.type, None)
if func isnotNone:
    tickrate += func()

The example above limits you to just manipulating the tickrate state, so you likely want something more robust. But the general form above where the event.type is used to fetch a function is quite useful.

Post a Comment for "Increment An Integer By Calling A Hashtable Value In Python 3?"