Objects¶
Over the past few weeks, we've used the word "object" frequently without defining exactly what it means. In this lesson, we'll introduce objects and see how we can use them in real data programming work. By the end of this lesson, students will be able to:
- Define a Python class to represent objects with specific states and behaviors.
- Explain how the Python memory model allows multiple references to the same objects.
- Add type annotations to variables, function definitions, and class fields.
import pandas as pd
An object (aka instance) in Python is a way of combining into a distinct unit (aka encapsulating) two software concepts:
- State, or data like the elements of a list.
- Behavior, or methods like a function that can take a list and return the size of the list.
Recently, we've been using DataFrame
objects frequently. A DataFrame
stores data (state) and has many methods (behaviors), such as groupby
.
seattle_air = pd.read_csv("seattle_air.csv", index_col="Time", parse_dates=True)
seattle_air.groupby(seattle_air.index.year).count()
Reference semantics¶
When we call a method like groupby
and then count
each group, the result is a new object that is distinct from the original. If we now ask for the value of seattle_air
, we'll see that the original DataFrame
is still there with all its data intact and untouched by the groupby
or count
operations.
seattle_air
However, unlike groupby
, there are some DataFrame
methods that can modify the underlying DataFrame
. The dropna
method for removing NaN
values can modify the original when we include the keyword argument inplace=True
(default False
). Furthermore, if inplace=True
, dropna
will return None
to more clearly communicate that instead of returning a new DataFrame
, changes were made to the original DataFrame
.
seattle_air.dropna()
seattle_air
Defining classes¶
Python allows us to create our own custom objects by defining a class: a blueprint or template for objects. The pandas
developers defined a DataFrame
class so that you can construct DataFrame
objects to use. Here's a highly simplified outline of the code that they could have written to define the DataFrame
class.
class DataFrame:
"""Represents two-dimensional tabular data structured around an index and column names."""
def __init__(self, index, columns, data):
"""Initializes a new DataFrame object from the given index, columns, and tabular data."""
print("Initializing DataFrame")
self.index = index
self.columns = columns
self.data = data
def dropna(self, inplace=False):
""""
Drops all rows containing NaN from this DataFrame. If inplace, returns None and modifies
self. If not inplace, returns a new DataFrame without modifying self.
"""
print("Calling dropna")
if not inplace:
return DataFrame([...], [...], [...])
else:
self.columns = [...]
self.index = [...]
self.data = [...]
return None
def __getitem__(self, column_or_indexer):
"""Given a column or indexer, returns the selection as a new Series or DataFrame object."""
print("Calling __getitem__")
if column_or_indexer in self.columns:
return "Series" # placeholder for a Series
else:
return DataFrame([...], [...], [...])
Let's breakdown each line of code.
class DataFrame:
begins the class definition. We always name classes by capitalizing each word removing spaces between words.def __init__(self, index, columns, data):
defines a special function called an initializer. The initializer is called whenever constructing a new object. EachDataFrame
stores its own data in fields (variables associated with an object), in this case calledindex
,columns
, anddata
.def dropna(self, inplace=False):
defines a function that can be called onDataFrame
objects. Like the initializer, it also takes aself
parameter as well as a default parameterinplace=False
. Depending on the value ofinplace
, it can either return a newDataFrame
orNone
.def __getitem__(self, column_or_indexer):
defines a special function that is called when you use the square brackets for indexing.
Notice how every method (function associated with an object) always takes self
as the first parameter. The two special functions that we defined above are only "special" in the sense that they have a specific naming format preceded by two underscores and followed by two underscores. These dunder methods are used internally by Python to enable the convenient syntax that we're all used to using.
Just like how we need to call a function to use it, we also need to create an object (instance) to use a class.
example = DataFrame([0, 1, 2], ["PM2.5"], [10, 20, 30])
example["PM2.5"]
Another useful dunder method is the __repr__
method, which should return a string representing the object. By default, __repr__
just tells you the fully-qualified name of the object's class and the location it is stored in your computer memory. But we can make it much more useful by defining our own __repr__
method.
example
Type annotations¶
We've talked a lot about the types of each variable in the Python programs that we write, but we can also optionally write-in the type of each variable or return value as a type hint. In certain assessments, we'll use mypy
to check your type annotations. Let's read the Type hints cheat sheet and practice adding type annotations to our previous class definitions.
!pip install -q nb_mypy
%reload_ext nb_mypy
%nb_mypy mypy-options --strict
Version 1.0.5
Practice: Student
class¶
Write a Student
class that represents a UW student, where each student has a name
, a student number
, and a courses
dictionary that associates the name of each course to a number of credits. The Student
class should include the following methods:
- An initializer that takes the student number and the name of a file containing information about their schedule.
- A method
__getitem__
that takes astr
course name and returns theint
number of credits for the course. If the student is not taking the given course, returnNone
. - A method
get_courses
that returns a list of the courses the student is taking.
Consider the following file nicole.txt
.
CSE163 4
PHIL100 4
CSE390HA 1
The student's name
is just the name of the file without the file extension. The file indicates they are taking CSE163 for 4 credits, PHIL100 for 4 credits, and CSE390HA for 1 credit.
class Student:
"""Represents a UW student with a name, number, and courses dictionary."""
# Type annotations are ways to tell other programmers what the type of
# each parameter and each return value will be! Documentation also does
# a lot of this, but the advantage of using type annotations is that
# Python (or, rather, mypy) is able to systematically double check your
# work.
def __init__(self, number: int, filename: str):
"""Initializes a new Student instance with the given number and filename."""
# filename[:-4] gets the string "nicole" from "nicole.txt"
self._name = filename[:-4]
self._number = number
self._courses = {}
self._load_file()
def _load_file(self, filename: str):
# Load in the courses from the path
with open(filename) as f:
for line in f.readlines():
course, credits = line.split()
self.courses[course] = int(credits)
# To fix incompatible return types for None, we need to specify None
# can be returned by this function!
def __getitem__(self, course: str) -> int | None:
"""Returns the number of credits the student is taking the given course."""
if course not in self._courses:
return None
return self._courses[course]
# For "generic type list" errors, we need to specify the type of
# elements stored in the list using square brackets notation
def get_courses(self) -> list[str]:
"""Return the list of courses the student is taking."""
return list(self._courses)
def get_name(self) -> str:
return self._name
def get_number(self) -> int:
return self._number
def __repr__(self) -> str:
return f"Student({self._number}, '{self._name}.txt')"
nicole = Student(1234567, "nicole.txt")
for course in nicole.get_courses():
print(course, nicole[course])
CSE163 4 PHIL100 4 CSE390HA 1
Practice: University
class¶
Write a University
class that represents one or more students enrolled in courses at a university. The University
class should include the following methods:
- An initializer that takes the university name and, optionally, a list of
Student
objects to enroll in this university. - A method
enrollments
that takes returns all the enrolledStudent
objects sorted in alphabetical order by student name. - A method
enroll
that takes aStudent
object and enrolls them in the university.
Later, we'll add more methods to this class. How well does your approach stand up to changing requirements?
class University:
# self.name, self.students
# Fix mutable default parameters: default to None and then reassign later.
def __init__(self, name: str, students: list[Student] | None = None):
self.name = name
if students is None:
students = []
self.students = students
self.courses : dict[str, list[Student]] = {}
for student in self.students:
for course in student.get_courses():
if course in self.courses:
self.courses[course].append(student)
else:
self.courses[course] = [student]
# # alternative 1
# if course not in self.courses:
# self.courses[course] = []
# self.courses[course].append(student)
# # alternative 2
# self.courses.get(course, []).append(student)
def enrollments(self) -> list[Student]:
# How to alphabetically sort the list of students?
return sorted(self.students, key=lambda student: student.name)
# alternative since the get_name function already takes one parameter
# return sorted(self.students, key=Student.get_name)
def enroll(self, student: Student) -> None:
self.students.append(student)
# need to update the self.courses
....
def roster(self, course: str) -> list[Student]:
"""
Return the students enrolled in the course.
"""
# enrolled_students = []
# for student in self.students:
# # check whether the student is enrolled in this course:
# if course in student.get_courses():
# enrolled_students.append(student)
# return enrolled_students
# precomputation approach
if course not in self.courses:
return []
return self.courses[course]
# return self.courses.get(course, [])
uw = University("Udub", [nicole])
# uw.enrollments()
print(uw.roster("CSE000"))
print(uw.roster("CSE163"))
print(uw.roster("CSE163"))
[] [Student(1234567, 'nicole.txt')]
student_list = [nicole, Student(1234568, 'nicole.txt'), Student(0, 'nicole.txt'), Student(-100000, 'nicole.txt')]
sorted(student_list, key=lambda student: student.get_number())
[Student(-100000, 'nicole.txt'), Student(0, 'nicole.txt'), Student(1234567, 'nicole.txt'), Student(1234568, 'nicole.txt')]
def get_number(student: Student) -> int:
return student.number
sorted(student_list, key=get_number, reverse=True)
[Student(1234568, 'nicole.txt'), Student(1234567, 'nicole.txt'), Student(0, 'nicole.txt'), Student(-100000, 'nicole.txt')]
Mutable default parameters¶
Default parameter values are evaluated and bound to the parameter when the function is defined. This can lead to some unanticipated results when using mutable values like lists or dictionaries as default parameter values.
Say we make two new University
objects without specifying a list of students to enroll. The initializer might then assign this list value to a field.
wsu = University("Wazzu")
wsu.enrollments()
[]
seattle_u = University("SeattleU")
seattle_u.enrollments()
[]
When we enroll a student to seattle_u
, the change will also affect wsu
. There are several ways to work around this, with the most common approach changing the default parameter value to None
and adding an if
statement in the program logic.
seattle_u.enroll(nicole)
seattle_u.enrollments()
[Student(1234567, 'nicole.txt')]
# Not expected: enrolling a student in SeattleU also enrolls them in WSU
wsu.enrollments()
[Student(1234567, 'nicole.txt')]