-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbank2.py
More file actions
executable file
·39 lines (33 loc) · 1.24 KB
/
bank2.py
File metadata and controls
executable file
·39 lines (33 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class BankAccount():
def __init__(self, name, initial_balance):
self.name = name
self.balance = initial_balance
'representation of the object "feedable" to Python interpreter'
def __repr__(self):
return self.__class__.__name__ + '(' + repr(self.name) \
+ ', ' + repr(self.balance) + ')'
''''string representation of object, for humans
__repr__ is used if __str__ does not exist'''
def __str__(self):
print('in the __str__() function')
return self.name + ' ' + str(self.balance)
def __add__(self, other):
return BankAccount(self.name + ' ' + other.name, \
self.balance + other.balance)
def __len__(self):
return self.balance
def deposit(self, amount):
if amount > 0:
self.balance += amount
return self.balance
else:
print("can't deposit nonpositive amount!")
def withdraw(self, amount):
if amount > 0:
if amount <= self.balance:
self.balance -= amount
return self.balance
else:
print("can't withdraw", amount, "or you would be overdrawn!")
else:
print("can't withdraw nonpositive amount!")