A java hobbyist programmer learning python

elhombre elhmbre at ozemail.com.au
Sun Jan 18 08:12:45 CET 2009


"Chris Rebert" <clp2 at rebertia.com> wrote in message 
news:mailman.7468.1232242083.3487.python-list at python.org...
> On Sat, Jan 17, 2009 at 5:11 PM, elhombre <elhmbre at 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. 




More information about the Python-list mailing list