forked from mozillascience/studyGroup
-
Notifications
You must be signed in to change notification settings - Fork 123
/
code_along.py
92 lines (68 loc) · 2.18 KB
/
code_along.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 12 17:19:42 2016
"""
class Client(object):
bank = "TD"
location = "Toronto"
def __init__(self, name, balance):
self.name = name
self.balance = balance + 100
if self.balance < 5000:
self.level = "Basic"
elif self.balance < 15000:
self.level = "Intermediate"
else:
self.level = "Advanced"
print "Name: %s, Opening Balance: %s" % (self.name, self.balance)
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount > self.balance:
raise RuntimeError("Insufficient Funds")
else:
self.balance -= amount
return self.balance
@staticmethod
def make_money_sound():
print "Cha-Ching!"
@classmethod
def bank_location(cls):
print "%s %s" % (cls.bank, cls.location)
John_Doe = Client("John Doe", 2000)
Jane_Defoe = Client("Jane Defoe", 10000)
John_Doe.name
John_Doe.balance
Jane_Defoe.level
John_Doe.email = "jdoe23@gmail.com"
John_Doe.email = "johndoe23@gmail.com"
del John_Doe.email
# try calling these attributes at the class and instance level
Client.bank
Jane_Defoe.location
# Try calling a method two different ways
John_Doe.withdraw(100)
Client.withdraw(John_Doe, 100)
Client.make_money_sound()
Jane_Defoe.make_money_sound()
Client.bank_location()
class Savings(Client):
interest_rate = 0.001
def update_balance(self):
self.balance += self.balance*self.interest_rate
return self.balance
# create an instance the same way as a Client but this time by calling Savings
# instead
Lina_Tran = Savings("Lina Tran", 50)
# it now has access to the new attributes and methods in Savings...
Lina_Tran.update_balance()
# ...as well as access to attributes and methods from the Client class
Lina_Tran.deposit(200)
print Lina_Tran.name
print Lina_Tran.balance
#defining a method outside the class definition
def check_balance(self):
return self.balance
Client.check_balance = check_balance
John_Doe.check_balance()