0

Just a simple program to deposit and withdraw from an account. I'm trying to learn classes by just testing them out.

class bank:
 def __init__(self):
 self.origBal = 0
 def deposit(self, amount):
 self.origBal += amount
 def withdraw(self, amount):
 self.origBal -= amount
b = bank()
d = bank()
w = bank()

That problem I'm having is probably best seen from the output.

For example, here is the output.

w.withdraw(3423)
b.origBal
-3423
d.deposit(3423)
b.origBal
-3423
d.deposit(322423)
d.origBal
325846
d.deposit(3223)
d.origBal
329069
w.withdraw(324334)
b.origBal
-3423
w.withdraw(234)
b.origBal
-3423

Not exactly sure what's happening.

I'm sure I could fix it by just manually entering (-n) or (+n) and only have one method, but I'd like to avoid that.

asked Oct 7, 2012 at 2:12
4
  • What is the problem with the output you posted? What are you expecting it to do instead? Commented Oct 7, 2012 at 2:14
  • Damn thing wouldn't format properly, the problem is the withdraw stays the same, even after I've deposited. I addded another at the end to make it more obvious. Commented Oct 7, 2012 at 2:15
  • 2
    I don't get what you're doing. Why are you creating three separate objects? If you do w.withdraw(3423) that withdraws from w. It certainly won't affect b or d, because those are different objects. Is that what you're trying to do? Give an example of the output you want. Commented Oct 7, 2012 at 2:16
  • Oh.... Seems I don't get classes yet then. I wanted to add or subtract from origBal. So my output would be: w.withdraw(3423) b.origBal -3423 d.deposit(3423) b.origBal -6846 d.deposit(846) d.origBal -6000 Sorry about this, I can't format this as code in the comment.... Commented Oct 7, 2012 at 2:20

2 Answers 2

2

When you do b = bank() you create one bank. When you do d = bank() you create a second bank. When you do w = bank() you create a third bank. Each bank has its own origBal. Calling deposit or withdraw on one of the three objects won't affect either of the other two objects. If you do

b = bank()
b.deposit(10)
b.withdraw(100)

. . . then things should work as you seem to expect.

You should read the Python tutorial to learn how classes work.

answered Oct 7, 2012 at 2:22
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, okay, I tried reading that and I couldn't understand it. Maybe I will now that I've learnt a little bit more. Sorry about all these formatting problems. Thanks mate.
1

To do what you want you need to use class variables not object variables. These are defined and used by the class as a whole as seen below.

class Bank(object):
 origBal = 0
 def deposit(self, amount):
 Bank.origBal += amount
 def withdraw(self, amount):
 Bank.origBal -= amount
b = Bank()
d = Bank()
w = Bank()
w.withdraw(3423)
b.origBal
-3423
d.deposit(3423)
b.origBal
0
d.deposit(322423)
b.origBal
322423
d.deposit(3223)
b.origBal
325646
w.withdraw(324334)
b.origBal
1312
w.withdraw(234)
b.origBal
1078
answered Oct 7, 2012 at 2:27

4 Comments

Is there benefit to this way over what BrenBarn suggested? I see you took out def init__(self): and put origBal=0 there. That's what I had at first, but I wasn't calling class variables, which is where I must've stuffed up, and someone told me to use the __init.
It really depends on what you are designing and what you expect your class to do. If you are keeping an overall running total then this way. If on the other hand you always expect a 0 balance when you create a new object of the class then @BrenBarn is correct.
You can do it this way, but it's sort of a weird way to use classes. In general a class is supposed to represent a "kind of thing" not an individual thing. So you're sort of working against the way Python classes are meant to be used. Conceptually there is no reason why you need to refer to one bank with three different names (b, d, and w), so why make three objects?
I was thinking the goal was more accounts in a bank so Sally = Bank(), Jim = Bank() and so forth but really that is down to how it is designed. The intent was just to expose the poster to the options available to them when working with classes. @BrenBarn

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.