Mapping operator tokens to special methods
Steven Bethard
steven.bethard at gmail.com
Thu Feb 24 14:17:38 EST 2005
jamesthiele.usenet at gmail.com wrote:
> I was starting to write a dictionary to map operator strings to their
> equivalent special methods such as:
> {
> '+' : 'add',
> '&' : 'and_'
> }
>
> The idea is to build a simple interactive calculator.
>
> and was wondering if there is already something like this builtin?
>
> Or is there a better way to do what I want?
There's not already a mapping builtin, but you should definitely look at
the operator module:
py> import operator
py> ops = {'+':operator.add, '&':operator.and_}
py> ops['+'](3, 2)
5
py> ops['&'](3, 2)
2
STeVe
More information about the Python-list
mailing list