[Tutor] __setitem__ [classes and functions]

Gregor Lingl glingl at aon.at
Fri Feb 27 18:20:46 EST 2004



Danny Yoo schrieb:...

>>>>def math_eval(operation, x, y):
>>>>        
>>>>
>...     dispatch_table = { '+': add,
>...                        '-': sub }
>...     if operation in dispatch_table:
>...         operator = dispatch_table[operation]
>...         return operator(x, y)
>...     else:
>...         print "I don't know about", operation
>...         return None
>...
>  
>
>>>>math_eval('+', 3, 17)
>>>>        
>>>>
>20
>  
>
>>>>math_eval('-', 49, 17)
>>>>        
>>>>
>32
>  
>
>>>>math_eval('-', 49, 7)
>>>>        
>>>>
>42
>###
>
>uses a dictionary whose keys are functions.  (Sorry for the detour!
>*grin*)
>  
>
I only see functions as *values* of the dictionary... or what do you mean?
(BTW, do functions count as "immutable objects", so they in principle can be used as dictionary keys?)

Gregor




More information about the Tutor mailing list