[Tutor] account class
Remco Gerlich
scarblac@pino.selwerd.nl
Sun, 23 Jul 2000 01:56:52 +0200
On Sat, Jul 22, 2000 at 07:42:25PM -0400, Steven Gilmore wrote:
> I'm trying to write a class that models a bank account. I've started simple
> with a deposit method, a print statement method, and balance property (I
> haven't gotten to the interest yet). Maybe I'm missing something though I
> can't figure out what. Since you guys/gals have been helpful so far maybe
> somebody could shed so light.
>
> Tell me it I have got this right
> (exaggerated comments)
>
> class Account:
> #when a new instance of this class is created, this line is called #
> def __init__(self):
> self.balance = 0.0
> #this sets the balance property to the value of the argument#
> def deposit(self,balance):
> self.balance= balance
> #prints a statement #
> def statement():
> print balance
>
> >>> import account
> >>> a = account.Account()
> >>> a.deposit(1.0)
> >>> a.statement()
> Traceback (innermost last):
> File "<interactive input>", line 0, in ?
> TypeError: no arguments expected
>
> hope that's enough info for ya to help me :)
Just change that into
def statement(self):
print self.balance
Every method *must* have the instance as its first arguments, since
the construct "instance.method()" (like "a.statement()") silently
sends that argument. You'll get used to it :-)
--
Remco Gerlich, scarblac@pino.selwerd.nl