"Chris Rebert" <c...@rebertia.com> wrote in message news:mailman.7468.1232242083.3487.python-l...@python.org...
On Sat, Jan 17, 2009 at 5:11 PM, elhombre <elhm...@ozemail.com.au> wrote:
...

Rather than have a long if-elif-else chain like this, you can use a
dictionary with functions as values. For example:

def add(x, y):
   return x + y

def sub(x, y):
   return x - y

OPER2FUNC = {'+' : add, '-' : sub}
print OPER2FUNC['+'](3,4) #==> 7

You can use the same technique with the functions in the `operator`
module to significantly shorten your calculate() method.

Cheers,
Chris

Thanks very much Chris. That is much more concise ! I have a lot of reading to do and a different mindset to work towards.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to