A java hobbyist programmer learning python

Rhodri James rhodri at wildebst.demon.co.uk
Mon Jan 19 19:53:09 EST 2009


On Sun, 18 Jan 2009 02:24:51 -0000, Steven D'Aprano  
<steve at remove-this-cybersource.com.au> wrote:

> Let me re-write your code in a more Pythonic way. This is not the only
> way to do this, and it probably isn't the best way, but it may give you a
> flavour for the way Python is usually written.
>
>
> import sys
> import operator

> class Calculator():
>     dispatch = {  # dispatch table mapping symbol to function
>         '+': operator.add,
>         '-': operator.sub,
>         '*': operator.mul,
>         '/': operator.truediv,
>         }
>     def __init__(self):
>         self.operator = sys.argv[1]
>         self.arg1 = int(sys.argv[2])
>         self.arg2 = int(sys.argv[3])

If you want the test code to work, I think you mean:

     def __init__(self, op, arg1, arg2):
         self.operator = op
         self.arg1 = arg1
         self.arg2 = arg2

:)
-- 
Rhodri James *-* Wildebeeste Herder to the Masses



More information about the Python-list mailing list