Making our own Classes and objects As in real life, we’re now creating classes of objects. a class that defines basic characteristics and functions that apply to all objects of that class Think of a class as a set of functions and variables that belong together to define a particular thing. E.g., Savings Account classes would have: A balance #variable, or attribute An owner’s name #variable, or attribute The ability to add money to the account #function, or method The ability to withdraw money from the account #function, or method An individual’s Savings Account might have: $ as a balance Roderick Feckelbocker as the owner’s name
Like creating our own Types Think of lists: Lists are a type. They have methods (functions) associated with them E.g., alist.append(x) alist.index(x) alist.pop() Append(), index(), and pop() are examples of methods (functions) associated with lists. You can’t use the pop() method (function) with something of type int, right?
If we were to write this: class OurList(object): def __init__(self, alist ): self.thisList = alist # a property! def ourPop(self): # Notice this is still part of the OurList class! x = self.thisList[len(self.thisList) – 1] self.thisList = self.thisList[0:len(self.thisList)-1] return(x)
To create something of the class OurList: class OurList(object): def __init__(self, alist ): self.thisList = alist # a property! def ourPop(self): x = self.thisList[len(self.thisList) – 1] self.thisList = self.thisList[0:len(self.thisList)-1] return(x) ls = OurList([3,2,4,1,5]) # Note that now we have to tell the computer what Class type we’re creating y = ls.ourPop()
Let’s make a class for Rectangles: What methods do we want our rectangle class to have? What properties? Class Rectangle(object): def __init__(self,h,w): self.height = h self.width = w def area(self): return(self.height * self.width)
Let’s make a class for Rectangles: What methods do we want our rectangle class to have? What properties? Class Rectangle(object): def __init__(self,h,w): self.height = h self.width = w def area(self): return(self.height * self.width) rect1 = Rectangle(10, 10) rect2 = Rectangle(20, 5) print(rect1.area())
class Rectangle(object): def __init__(self, width, height): #[CONSTRUCTOR] self.width = width self.height = height def area(self): return self.width * self.height def boxvol(self, x ): return self.width * self.height * x rectangle1 = Rectangle(10, 10) #[EXAMPLES] rectangle2 = Rectangle(20, 5) print(rectangle1.area()) print(rectangle2.boxvol(2)) print(Rectangle(6,8).area())
Classes Does the problem involve data that is related to a single concept? (e.g., a student, an airline ticket, a car) Write down the kinds of data in the problem and organize them into classes (some problems may need multiple classes). Write: class definition a list of properties belonging to objects of that class a constructor (__init__) An example function some example class instances.
Employee Class class Employee: def __init__(self, n, s): self.name = n self.salary = s def displayEmployee(self): print("Name : "+self.name+", Salary: "+ self.salary) x = Employee(“bob”,32000) x.displayEmployee()
Employee Class class Employee: def __init__(self, n, s): self.name = n self.salary = s def displayEmployee(self): print("Name : "+self.name+", Salary: "+ self.salary) def getRaise(self,x): self.salary = self.salary + x x = Employee(“bob”,32000) x.displayEmployee() x.getRaise(2000) x.displayEmployee()
Student Class? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project def getgrade(self): x= self.lab * self.exam * self.project * 0.25 return(x)
Write a class for a circle? Functions? Properties?
class Circle(object): """ A circle is a shape with radius, a circumference, and an area radius - number circumference – number (2*pi*radius) area – number (pi*radius**2) """ def __init__(self, x,y,z): self.radius = x self.area=y self.circumference=z x= Circle(3,7,8)
class Circle(object): def __init__(self,x): self.radius = x self.circumference = self.getcirc() self.area = 7 def getcirc(self): return(self.radius * 2 * pi) def area(self): return(self.radius **2 * pi) def changerad(self,x): self.radius = x self.circumference = self.getcirc() circ1 = Circle(3) Print(circ1.area) circ1.changerad(4) Print(circ1.area()) Can we include in the class a function that checks if this circle is bigger than another circle? What type should it return?
from math import * class Circle(object): def __init__(self,x): self.radius = x self.circumference = self.getcirc() self.area = self.getarea() def getcirc(self): return(self.radius * 2 * pi) def getarea(self): return(self.radius **2 * pi) def isbigger(self, circ2): return(self.area > circ2.area) firstcirc = Circle(3) secondcirc = Circle(4) thirdcirc = Circle(2) print(firstcirc.area) print(firstcirc.isbigger(secondcirc)) print(firstcirc.isbigger(thirdcirc))
Student – what if we wanted a grade property? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade() def getgrade(self): x= self.lab * self.exam * self.project * 0.25 return(x) Now make a student. Print his/her grade.
Printing a student object? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade() def getgrade(self): x= self.lab * self.exam * self.project * 0.25 return(x) def __str__(self): return(self.lastname + ","+self.firstname+": \n\tLab: "+\ str(self.lab) + "\n\tExam: " + str(self.exam) +\ "\n\tProject: "+str(self.project)+"\n\tGrade: "+str(self.grade)) x = Student('Bill','Williams',88,76,94) print(x) y = Student('Anne','Stewart',97,67,73) print(y)
What about comparing students? class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade() def getgrade(self): x= self.lab * self.exam * self.project * 0.25 return(x) def __str__(self): return(self.lastname + ","+self.firstname+": \n\tLab: "+\ str(self.lab) + "\n\tExam: " + str(self.exam) +\ "\n\tProject: "+str(self.project)+"\n\tGrade: "+str(self.grade)) def __lt__(self,x): if (self.lastname != x.lastname): return self.lastname < x.lastname elif (self.firstname != x.firstname): return(self.firstname < x.firstname) else: return(self.grade < x.grade)
class Student(object): def __init__(self,firstname,lastname,lab,exam,project): self.firstname = firstname self.lastname = lastname self.lab = lab self.exam = exam self.project = project self.grade = self.getgrade() def getgrade(self): x= self.lab * self.exam * self.project * 0.25 return(x) def __str__(self): return(self.lastname + ","+self.firstname+": \n\tLab: "+\ str(self.lab) + "\n\tExam: " + str(self.exam) +\ "\n\tProject: "+str(self.project)+"\n\tGrade: "+str(self.grade)) def __lt__(self,x): if (self.lastname != x.lastname): return self.lastname < x.lastname elif (self.firstname != x.firstname): return(self.firstname < x.firstname) else: return(self.grade < x.grade) x = Student("Robert","Jones",64,71,86) print(x) y = Student("Tom","Miller",72,44,87) print(y) z = Student("Anne","Jones",87,78,92) x = Student("Robert","Jones",64,71,86) print(x) y = Student("Tom","Miller",72,44,87) print(y) z = Student("Anne","Jones",87,78,92) if (x < y): print('hi') else: print('low') if (x < z): print('hi2') else: print('low2')
Others: __eq__(self, other) Defines behavior for the equality operator, ==. __ne__(self, other) Defines behavior for the inequality operator, !=. __lt__(self, other) Defines behavior for the less-than operator, <. __gt__(self, other) Defines behavior for the greater-than operator, >. __le__(self, other) Defines behavior for the less-than-or-equal-to operator, <=. __ge__(self, other) Defines behavior for the greater-than-or-equal-to operator, >=.