1 / 8

Python Classes

Python Classes. 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)

marcy
Download Presentation

Python Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Classes

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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) ,

More Related