Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Philip Cannata 1 Python Classes. Dr. Philip Cannata 2.

Similar presentations


Presentation on theme: "Dr. Philip Cannata 1 Python Classes. Dr. Philip Cannata 2."— Presentation transcript:

1 Dr. Philip Cannata 1 Python Classes

2 Dr. Philip Cannata 2

3 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_kids a1 = animal(100,5) print a1.num_eyes, a1.num_teeth, a1.num_children print 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 = 28 my_cat.num_children = 4 print my_cat.num_eyes, my_cat.num_teeth, my_cat.num_children, my_cat.color_fur

4 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_kids a1 = animal(100,5) print a1.num_eyes, a1.num_teeth, a1.num_children print 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

5 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].math print socrates.knowledge print socrates_Knowledge.grammatical, socrates_Knowledge.math, socrates_Knowledge.human[0].sports_played

6 Dr. Philip Cannata 6 Code from September 17, 2013 Lecture class 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 = j1 j1.employees.append(p1) print p1.has.name, j1.employees[0].name

7 Dr. Philip Cannata 7 Code from September 17, 2013 Lecture class 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) print for p in Person._registry: print p.name + ", " + str(p.amount), # More on Next Page

8 Dr. Philip Cannata 8 Code from September 17, 2013 Lecture p1.exchange(p2, 50) print for p in Person._registry: print p.name + ", " + str(p.amount), p10 = Person("me", "you") print print p10._registry print print 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. print for p in Person._registry: print p.name + ", " + str(p.amount),


Download ppt "Dr. Philip Cannata 1 Python Classes. Dr. Philip Cannata 2."

Similar presentations


Ads by Google