A java hobbyist programmer learning python
elhombre
elhmbre at ozemail.com.au
Sun Jan 18 02:16:39 EST 2009
"John Machin" <sjmachin at lexicon.net> wrote in message
news:5d2c588a-9b01-4a85-85b2-b132754e6e08 at o40g2000prn.googlegroups.com...
> 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
Thanks John.
>
More information about the Python-list
mailing list