Mapping operator tokens to special methods

J

jamesthiele.usenet

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?
 
S

Steven Bethard

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 = {'+':eek:perator.add, '&':eek:perator.and_}
py> ops['+'](3, 2)
5
py> ops['&'](3, 2)
2

STeVe
 
J

John Machin

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?
3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,221
Messages
2,571,131
Members
47,747
Latest member
swapote

Latest Threads

Top