Dr. Philip Cannata 1 Data Management. Dr. Philip Cannata 2 Syllabus.
Dr. Philip Cannata 1 Python Classes. Dr. Philip Cannata 2.
-
Upload
stanley-neal -
Category
Documents
-
view
224 -
download
0
Transcript of Dr. Philip Cannata 1 Python Classes. Dr. Philip Cannata 2.
Dr. Philip Cannata 1
Python Classes
Dr. Philip Cannata 2
Dr. Philip Cannata 3
Python class code for Aristotle example – animal and cat classes
class animal(object) : num_eyes = 2 num_teeth = 0 num_children = 0 def __init__(self, num_t, num_kids) : self.num_teeth = num_t self.num_children = num_kidsa1 = animal(100,5)print a1.num_eyes, a1.num_teeth, a1.num_childrenprint a1
class cat(animal) : color_fur = "brown" len_tail = 3 def __init__(self, fur, tail) : self.color_fur = fur self.len_tail = tail
my_cat = cat("yellow", 4)my_cat.num_teeth = 28my_cat.num_children = 4print my_cat.num_eyes, my_cat.num_teeth, my_cat.num_children, my_cat.color_fur
Dr. Philip Cannata 4
Python class code with “super” in constructor for Aristotle example - animal and cat classes
class animal(object) : num_eyes = 2 num_teeth = 0 num_children = 0 def __init__(self, num_t, num_kids) : self.num_teeth = num_t self.num_children = num_kidsa1 = animal(100,5)print a1.num_eyes, a1.num_teeth, a1.num_childrenprint a1
class cat(animal) : def __init__(self, num_t, num_kids, fur, tail) : super(cat,self).__init__(num_t, num_kids) self.color_fur = fur self.len_tail = tail
my_cat = cat( 34, 2, 23,2)print my_cat.num_eyes, my_cat.num_teeth, my_cat.num_children, my_cat.color_fur, my_cat.len_tail
Dr. Philip Cannata 5
Python class code for Aristotle example – animal, cat, human, and knowledge classes
class animal() : num_eyes = 2 num_teeth = 24 def __init__(self, e, t) : self.num_eyes = e self.num_teeth = t class cat(animal) : def __init__(self, c, l) : self.fur_color = c self.len_tail = l a1 = animal(2, 30)print a1.num_eyes, a1.num_teeth
my_cat = cat("yellow", 4)print my_cat.num_eyes, my_cat.num_teeth, my_cat.fur_color, my_cat.len_tail
class human(animal) : IQ = 100 sports_played = "Football" knowledge = [] def __init__(self, s, i = 100) : self.IQ = i self.sports_played = s
socrates = human(s="Golf")print socrates.num_eyes, socrates.num_teeth, socrates.IQ, socrates.sports_played
class knowledge() : grammatical = '' math = '' human = [] def __init__(self, g, m) : self.grammatical = g self.math = m
socrates_Knowledge = knowledge('Some Greek language knowledge', "Some math smarts")socrates.knowledge.append(socrates_Knowledge)socrates_Knowledge.human.append(socrates)print socrates.num_eyes, socrates.num_teeth, socrates.IQ, socrates.sports_played, socrates.knowledge[0].grammatical, socrates.knowledge[0].mathprint socrates.knowledgeprint socrates_Knowledge.grammatical, socrates_Knowledge.math, socrates_Knowledge.human[0].sports_played
Dr. Philip Cannata 6
Code from September 17, 2013 Lectureclass Person() : name = None parents = [] kids = [] has = None def __init__(self, name) : self.name = name
p1 = Person("phil")p2 = Person("rita")p3 = Person("chris")p1.kids.append(p3)p3.parents.append(p1)
print "My name is ", p1.name, "My first kids name is ", p1.kids[0].name
class Job(): name = "" employees = [] def __init__(self, name) : self.name = name j1 = Job("professor")j1.employees = [] # This was added after class.p1.has = j1j1.employees.append(p1)
print p1.has.name, j1.employees[0].name
Dr. Philip Cannata 7
Code from September 17, 2013 Lectureclass Person(object): _registry = [] # _registry starts off as an empty list. name = "" amount = 0
def __init__(self, name, amount): self._registry.append(self) # Each instance of Person is added to _registry. self.name = name self.amount = amount def exchange(self, p, amnt) : # Behavior or Method self.amount -= amnt p.amount += amnt
p1, p2, p3, p4 = Person('tom', 1000), Person('jerry', 2000), Person('phineas', 3000), Person('ferb', 4000)for p in Person._registry: print p.name + ", " + str(p.amount) , # The comma at the end of the print statement causes all to print on one line.
def transfer(p1, p2, amnt) : p1.amount -= amnt # Fill in these 2 lines for Homework 1. Note, the “transfer” function p2.amount += amnt # requires no return statement.
transfer(p1, p2, 50)transfer(p3, p4, 50)
printfor p in Person._registry: print p.name + ", " + str(p.amount) , # More on Next Page
Dr. Philip Cannata 8
Code from September 17, 2013 Lecture p1.exchange(p2, 50)
printfor p in Person._registry: print p.name + ", " + str(p.amount) , p10 = Person("me", "you")printprint p10._registryprintprint p1._registry
class MeekPerson(Person) : def __init__(self, name, amount): self._registry.append(self) # Each instance of Person is added to _registry. self.name = name self.amount = amount def exchange(self, p, amnt) : self.amount -= 2*amnt p.amount += 2*amnt
m1 = MeekPerson("snoopy", 10000)p6 = Person("charlie", 20000)m1.exchange(p6,50) # Dynamic Binding will be done here to choose the right method.printfor p in Person._registry: print p.name + ", " + str(p.amount) ,