In [ ]:
# create a class
class Subject:
pass
In [ ]:
S1 = Subject()
S1.fname = "Jane"
S1.lname = "Doe"
S1.ID = 111
S1.date_of_admission ="2019_01_01"
S2 = Subject()
S2.fname = "John"
S2.lname = "Smith"
S2.ID = 112
S2.date_of_admission = "2019_01_09"
In [ ]:
print(S1.fname,S1.lname)
In [ ]:
class Subject:
def fullname(self):
print(self.fname,self.lname)
In [ ]:
S2.fullname()
In [ ]:
class Subject:
def __init__(self, fname, lname, ID, date_of_admission):
self.fname = fname
self.lname = lname
self.ID = ID
self.date_of_admission = date_of_admission
def fullname(self):
print(self.fname,self.lname)
In [ ]:
S1 = Subject("Jane", "Doe", 111, "2019_01_01")
S2 = Subject("John", "Smith", 112, "2019_01_09")
In [ ]:
S1.fullname()
In [ ]:
S1.reletives = S2
In [ ]:
S1.reletives.fullname()
In [ ]:
# example of a bank client database
# create the Client class
class Client(object):
def __init__(self, name, balance):
self.name = name
self.balance = balance + 100
# define account level
if self.balance < 5000:
self.level = "Basic"
elif self.balance < 15000:
self.level = "Intermediate"
else:
self.level = "Advanced"
In [ ]:
# create two instances of this class (i.e. objects)
C1 = Client("John Doe", 500)
C2 = Client("Jane Defoe", 150000)
In [ ]:
C2.level
In [ ]:
C1.email = "Jdoe@gmail.com"
C2.email = "JaneDd@gmail.com"
In [ ]:
del C1.email
In [ ]:
Client.bank = "TD"
Client.location = "Toronto, ON"
In [15]:
# create some methods for this class: update the object upon withdraw or deposit of money
class Client():
def __init__(self, name, balance):
self.name = name
self.balance = balance + 100
# define account level
if self.balance < 5000:
self.level = "Basic"
elif self.balance < 15000:
self.level = "Intermediate"
else:
self.level = "Advanced"
def deposit(self, amount):
self.balance += amount
# define account level
if self.balance < 5000:
self.level = "Basic"
elif self.balance < 15000:
self.level = "Intermediate"
else:
self.level = "Advanced"
return self.balance
def withdraw(self, amount):
if amount > self.balance:
raise RuntimeError("Insufficient for withdrawal")
else:
self.balance -= amount
return self.balance
In [21]:
C1 = Client("John Doe", 500)
C2 = Client("Jane Defoe", 150000)
In [17]:
C1.deposit(150000)
Out[17]:
In [18]:
C1.level
Out[18]:
In [19]:
# define static method for Client
class Client():
def __init__(self, name, balance):
self.name = name
self.balance = balance + 100
# define account level
if self.balance < 5000:
self.level = "Basic"
elif self.balance < 15000:
self.level = "Intermediate"
else:
self.level = "Advanced"
def deposit(self, amount):
self.balance += amount
# define account level
if self.balance < 5000:
self.level = "Basic"
elif self.balance < 15000:
self.level = "Intermediate"
else:
self.level = "Advanced"
return self.balance
def withdraw(self, amount):
if amount > self.balance:
raise RuntimeError("Insufficient for withdrawal")
else:
self.balance -= amount
return self.balance
@staticmethod
def make_money_sound():
print("Cha_Ching!")
In [20]:
Client.make_money_sound()
In [22]:
C1 = Client("John Doe", 500)
C2 = Client("Jane Defoe", 150000)
In [27]:
class Client(object):
bank = "TD"
location = "Toronto, ON"
def __init__(self, name, balance):
self.name = name
self.balance = balance + 100
# define account level
if self.balance < 5000:
self.level = "Basic"
elif self.balance < 15000:
self.level = "Intermediate"
else:
self.level = "Advanced"
def deposit(self, amount):
self.balance += amount
# define account level
if self.balance < 5000:
self.level = "Basic"
elif self.balance < 15000:
self.level = "Intermediate"
else:
self.level = "Advanced"
return self.balance
def withdraw(self, amount):
if amount > self.balance:
raise RuntimeError("Insufficient for withdrawal")
else:
self.balance -= amount
return self.balance
@classmethod
def bank_location(cls):
return str(cls.bank + " " + cls.location)
In [28]:
Client.bank_location()
Out[28]:
In [31]:
# create 'child' for Client class
class Savings(Client):
interest_rate = 0.005
def update_balance(self):
self.balance += self.balance*self.interest_rate
return self.balance
In [32]:
C3 = Savings("Tom Smith", 50)
In [35]:
C3.level
Out[35]:
In [36]:
def check_balance(self):
return self.balance
In [37]:
Client.check_balance = check_balance
In [38]:
C3.check_balance()
Out[38]:
In [1]:
class blah(Savings):
def update_blah(self):
self.balance += self.balance + 2
return self.balance
In [40]:
C4 = blah("TT", 100)
In [ ]:
C1.