Using an operator as an object

Cameron Laird claird at
Mon Mar 3 14:20:42 CET 2003

In article <b3v7ha01gvg at>,
Alex Martelli  <aleax at> wrote:
>Python library.  However, if you DO have a string representing
>an operator, an if/elif tree is still not the best way to find
>out which one of the functions in module operator you have to
>call -- rather, you're probably better off building a dictionary
>once only:
>import operator
>string_to_op = {
>    '+': operator.add,
>    '-': operator.sub,
>    '*': operator.mul,
>    '/': operator.div,
>    '%': operator.mod,
>    # and possibly others if you need them, of course
Is such a mapping not already exposed somewhere?
'Near as I can tell, the answer is, "No".  Would
this not be one of the benefits of the (several) 
project(s) to script more of Python's core in

Cameron Laird <Cameron at>

More information about the Python-list mailing list