Zhiguang Huo (Caleb)
Monday Oct 31st, 2022
class Person:
def setName(self, aname):
self.name = aname
def greet(self):
print("Hi there, my name is " + self.name)
a = Person()
a.setName("Lucas")
a.greet()
## Hi there, my name is Lucas
## Hi there, my name is Lucas
## Hi there, my name is Amy
## Hi there, my name is Beth
class Person:
def __init__(self):
self.name = "John"
def setName(self, aname):
self.name = aname
def greet(self):
print("Hi there, my name is " + self.name)
a = Person()
a.setName("Lucas")
a.greet()
## Hi there, my name is Lucas
## Hi there, my name is John
pseudocode for the eat method:
if hungry==True:
print("Aaah")
hungry=False
else:
print("No thanks")
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print("Aaah")
self.hungry = False
else:
print("No, thanks")
aBird = Bird()
aBird.eat()
## Aaah
## No, thanks
class Person:
def __init__(self, aname):
self.name = aname
def setName(self, aname):
self.name = aname
def greet(self):
print("Hi there, my name is " + self.name)
a = Person("Lucas")
b = Person("Amy")
a.greet()
## Hi there, my name is Lucas
## Hi there, my name is Amy
class Person:
def __init__(self, aname="John"):
self.name = aname
def setName(self, aname):
self.name = aname
def greet(self):
print("Hi there, my name is " + self.name)
a = Person("Lucas")
a.greet()
## Hi there, my name is Lucas
## Hi there, my name is John
class Bird0:
song = 'Squaawk'
def sing(self):
print(self.song)
def sing2():
print(song)
bird = Bird0()
bird.sing()
#bird.sing2()
## Squaawk
class Secretive:
def __inaccessible(self):
print("Bet you can't see me...")
def accessible(self):
print("The secret message is:")
self.__inaccessible()
s = Secretive()
#s.__inaccessible() ## does not work
s.accessible()
## The secret message is:
## Bet you can't see me...
class Person:
def __init__(self, aname):
self.name = aname
def getName(self):
return(self.name)
a = Person("Lucas")
## 'Lucas'
## 'Lucas'
class Person:
def __init__(self, aname):
self.__name = aname
def getName(self):
return(self.__name)
a = Person("Lucas")
a.getName()
#a.__name ## does not work
## 'Lucas'
pseudocode for the eat method:
if hungry==True:
print("Aaah")
hungry=False
else:
print("No thanks")
class Car:
def __init__(self, **kw):
self.make = kw["make"]
# self.model = kw["model"]
self.model = kw.get("model")
my_car = Car(make = "Chevo", model = "Malibu")
print(my_car.model)
## Malibu
## None
class Person:
def __init__(self, aname="John"):
self.name = aname
def setName(self, aname):
self.name = aname
def getName(self):
return(self.name)
def greet(self):
print("Hi there, my name is " + self.name)
class Group:
def __init__(self):
self.persons = []
def add(self, aperson):
self.persons.append(aperson)
def getNameAll(self):
res = [aperson.getName() for aperson in self.persons]
return(res)
agroup = Group()
agroup.add(Person("Amy"))
agroup.add(Person("Beth"))
agroup.add(Person("Carl"))
agroup.getNameAll()
## ['Amy', 'Beth', 'Carl']
class Child(Parent):
class Person:
def __init__(self):
self.name = "John"
def setName(self, aname):
self.name = aname
def greet(self):
print("Hi there, my name is " + self.name)
## Hi there, my name is John
The child class can have additional attributes and methods compared to the parent class
class Filter:
def __init__(self):
self.blocked = [0, 1]
def filter(self, sequence):
return [x for x in sequence if x not in self.blocked]
## [2, 3]
## ['bacan']
## True
## False
## [2, 3]
class SPAMFilter3(Filter):
def __init__(self):
self.blocked.append('SPAM')
s3 = SPAMFilter3()
s3.filter([1, 2, "SPAM"])
class Animal(object):
def __init__(self, animal_type):
print('Animal Type:', animal_type)
class Mammal(Animal):
def __init__(self):
# call superclass
super().__init__('Mammal')
## super(Mammal, self).__init__('Mammal') ## alternatively
print('Mammals give birth directly')
dog = Mammal()
## Animal Type: Mammal
## Mammals give birth directly
class Filter:
def __init__(self):
self.blocked = [0, 1]
def filter(self, sequence):
return [x for x in sequence if x not in self.blocked]
class SPAMFilter4(Filter):
def __init__(self):
super(SPAMFilter4, self).__init__()
self.blocked.append('SPAM')
## [2]
def sing(self):
print(self.sound * 3)
class Bird:
def __init__(self):
self.hungry = True
def eat(self):
if self.hungry:
print("Aaah")
self.hungry = False
else:
print("No, thanks")
class SongBird(Bird):
def __init__(self):
super(SongBird, self).__init__() ## need this to initialize the parental class
self.sound = "tweet"
def sing(self):
print(self.sound * 3)
## True
## 'tweet'
## tweettweettweet
## Aaah
## False
## No, thanks
class Calculator:
def calculate(self, expression):
self.value = eval(expression) ## eval is to evaluate some expression in python
class Talker:
def talk(self):
print("Hi, my value is", self.value)
class TalkingCalculator(Calculator, Talker):
pass
## Hi, my value is 2
SongBird2(color="yellow", hungry=True, sound = "TWEET")
def sing(self):
print(self.sound * 3)
class Bird2:
def __init__(self, color="yellow", hungry=True):
self.color = color
self.hungry = hungry
def getColor(self):
return(self.color)
def eat(self):
if self.hungry:
print("Aaah")
self.hungry = False
else:
print("No, thanks")
## bBird = Bird2("red")
## bBird.getColor()
## bBird.eat()
## bBird.eat()
## TWEETTWEETTWEET
## red
## Aaah
## No, thanks
## False
## True
## False
## True