32_MoreOOP
32_MoreOOP
Join at slido.com
#6705200
CS1301- Intro to Computing
Day 32
Please download and install the Slido
app on all computers you use
3
Review - OOP class type
A class definition represents to the design (blueprint) of
some object (instance)
• The design specifies the data attributes and methods of an object
of type<class>.
• data attributes or properties describe the object
• methods are tasks that the object can do or have done to it
• Specialized Python methods that begin with double underscores are
called dunder methods (_ _init_ _, _ _str_ _, _ _eq_ _, … )
Fall 2024 4
Review - OOP class definition
class Class_name: # class definition & design for an object of the class
def __init__(self):
# *REQUIRED* automatically executes for object creation
# assigns class attributes to __init__ parameters
# defines class methods
# default parameter self class object
def __str__(self):
# overrides the default str when print() for a class object
Fall 2024 5
Please download and install the Slido
app on all computers you use
Fall 2024 6
OOP – dunder comparison methods
• Defines the comparison of an attribute of object self and
object other
Fall 2024 7
Miniquiz
Fall 2024 8
OOP – dunder comparison methods should have a common attribute
class Car:
''' a type of car with a color and mileage '''
def __eq__(self,other): # equal
return self.color == other.color and self.mileage==other.mileage
def __lt__(self, other): # less than
return self.mileage < other.mileage
def __le__(self, other): # less than or equal
return self.mileage <= other.mileage
def __ne__(self, other): # not equal
return self.mileage != other.mileage
def __gt__(self, other): # greater than
return self.mileage > other.mileage
def __ge__(self, other): # greater than or equal
return self.mileage >= other.mileage
Fall 2024 9
class Car:
''' a type of car with a color and mileage '''
def __init__(self, color, mileage):
self.color = color
self.mileage = mileage
def __str__(self):
return f"The car is {self.color} with {self.mileage:,} miles."
class Student:
''' type student with name and GPA '''
def __init__(self,name,gpa):
self.name = name
self.gpa = gpa
def __str__(self):
return f"{self.name} has a {self.gpa} GPA.“
Josh = Student(‘Josh’,4.0)
myCar = Car(‘White’,150396)
Fall 2024 10
OOP – __str__ interaction
• Python calls __str__ based on the data type of the
object.
stuff = [Josh, myCar, ‘cat’, [1,3,2]]
for item in stuff:
print(item)
Josh has a 4.0 GPA # __str__ from <class Student>
The car is White with 150,396 miles. # __str__ from <class Car>
Cat # print() from standard library for <class Str>
[1,3,2] # print() from standard library for <class list>
Fall 2024 11
Object – default parameter value
• A default parameter value, allows for the creation of an object
without a given parameter.
• The default parameter value is assigned to the object attributes
class Student:
def __init__(self, p1, p2, p3 = "Undeclared"):
self.name = p1 # attribute value given by parameter
self.gpa = p2 # attribute value given by parameter
self.major = p3 # attribute value assigned the default given by parameter
self.courses = [] # attribute value assigned, not given by parameter
Fall 2024 12
OOP – class interaction
• The object of a class can be a data value of another class object
class Course:
def __init__(self,dept: str, courseNumber: str, creditHours: int):
self.dept = dept
self.number = courseNumber
self.hours = creditHours
def __str__(self):
return f"{self.dept}{self.number} is {self.hours} hours"
class Student:
def __init__(self, studentID: str): MATH1501 is 4 hours
CS1301 is 3 hours
self.gtid = studentID
self.courses = [] # default assignment of an empty list
Fall 2024 13
Time to program
• Launch IDLE (Python) on your laptop
• Using the Files tab in Canvas
• Select the Lecture Materials folder
• Select, download, and open: 32code.py
Fall 2024 14