-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbankeq.py
More file actions
51 lines (43 loc) · 1.58 KB
/
Copy pathbankeq.py
File metadata and controls
51 lines (43 loc) · 1.58 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
39
40
41
42
43
44
45
46
47
48
49
50
51
class BankAccount():
def __init__(self, name, initial_balance):
self.name = name
self.balance = initial_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!")
'representation of the object "feedable" to Python interpreter'
def __repr__(self):
return self.__class__.__name__ + '(' + repr(self.name) \
+ ', ' + repr(self.balance) + ')'
def __str__(self):
"""string representation of object, for humans
__repr__ is used if __str__ does not exist
"""
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 __eq__(self, other):
return self.balance == other.balance
if __name__=="__main__":
ritch = BankAccount("The King", 1000)
poor = BankAccount("The Fool", 1)
# example tests for __eq__
also_ritch = BankAccount("The Queen", 1000)
print(ritch == also_ritch)
print(poor == ritch)