A java hobbyist programmer learning python

John Machin sjmachin at lexicon.net
Sat Jan 17 21:04:47 EST 2009


On Jan 18, 12:11 pm, "elhombre" <elhm... at ozemail.com.au> wrote:
> Hello, below is my first fragment of working python code. As you can see it
> is very java like as that is all I know. Is this the right approach to be
> taking?
> Should I be taking a different approach? Thanks in advance.
>
> import sys
>
> class Calculator():
>
>     def __init__(self):
>         self.operator = sys.argv[1]
>         self.arg1 = sys.argv[2]
>         self.arg2 = sys.argv[3]

Try this:

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

Then you can do
x1 = Calculator('+', '1.0', '2.0')
x2 = Calculator('-', '666', '42')
or
x3 = Calculator(*sys.argv[1:4])
if you're really desperate to use the command line args.

>
>     def getOperator(self):
>         return sys.argv[1]

Chris has already told you to give such accessor functions the flick,
but it should have done
    return self.operator




More information about the Python-list mailing list