Stats 21 - Lecture 20 - Introducing OOP in Python
Overview
Miles Chen introduces Python classes as programmer-defined types that combine stored attributes with operations, using Point, Rectangle, and Time examples to explain instances, mutability, object identity, and copying. He compares pure functions with modifiers, shows how shallow and deep copies behave, and develops increasingly reliable time arithmetic using divmod and conversions to seconds before previewing more complete class definitions.
Key takeaways
- A Python class is a factory for instances: `Point` defines the type, while each call to `Point()` creates a separate object.
- Attributes belong to individual objects, so changing `blank.x` does not update a separate global variable that previously received the value of `blank.x`.
- A shallow copy duplicates the outer object but preserves references to nested objects; a deep copy duplicates those nested objects as well.
- Python modifiers can change objects without returning a value, so their effects may be less apparent and more difficult to debug than pure functions.
- Time arithmetic is easier to normalize when seconds and minutes are carried with `divmod`, or when times are converted to seconds after midnight, added as integers, and converted back.
Chapters
- Miles Chen returns to core Python programming after recent work with NumPy, pandas, and Matplotlib.
- A class defines a new type of object that can store information and support actions.
- The lecture uses Point, Rectangle, and Time examples, with fuller class definitions planned for Wednesday.
- The `class Point:` statement defines a class, with PascalCase used for class names.
- A triple-quoted docstring documents the class but does not add executable behavior.
- Calling `Point()` creates a new instance, like pressing a factory button to produce an object.
- An initially empty Point instance can receive attributes such as `x` and `y` through assignments like `blank.x = 3`.
- Reading `blank.y` retrieves the value stored on that object, while assigning it to a global variable creates a separate binding.
- Changing `blank.x` later does not change a previously assigned global `x`, because the values are stored in different places.
- Chen represents a Rectangle using `width`, `height`, and a `corner` attribute that references a Point instance.
- A 100-by-200 rectangle with corner `(0, 0)` contains a Point object nested inside the Rectangle object.
- A center-finding function adds half the width and height to the corner coordinates, producing `(50, 100)` for the example rectangle.
- Copying a Point with Python’s `copy` module creates a distinct instance; default equality for these custom objects checks identity rather than matching attributes.
- A shallow copy of a Rectangle creates a new outer object but keeps a reference to the same nested `corner` Point.
- Changing the shared corner’s `x` value affects both shallow-copied rectangles, while changing one rectangle’s own height does not affect the other.
- A deep copy creates a separate nested Point, so the copied rectangle and original have different corner objects.
- A Time instance stores `hour`, `minute`, and `second`; formatting with two-digit fields produces strings such as `11:59:30`.
- A pure function leaves its arguments unchanged and returns a result, while a modifier changes an argument and may return nothing.
- Adding times component by component can produce invalid values: 9:45 plus 1 hour 35 minutes initially yields 10:80 instead of 11:20.
- A first modifier that adds seconds fails for large increments, returning 9:47:155 after adding 185 seconds to 9:46:30.
- Using `divmod(seconds, 60)` carries whole minutes forward and keeps the remaining seconds in range; a second divmod handles minutes and hours.
- The corrected modifier adds 185 seconds to 9:45 as 9:48:05, then adds 4,800 seconds to reach 11:08:05.
- Modifiers are convenient but harder to debug because their effect is visible in the changed object rather than a returned value.
- The conversion function maps a Time to seconds after midnight by combining hours, minutes, and seconds.
- An inverse function uses divmod by 60 to reconstruct the hour, minute, and second fields from a total number of seconds.
- Adding 9:45 and a duration of 1 hour 35 minutes becomes conversion, integer addition, and conversion back, producing 11:20.
- Chen closes by noting that the demonstrated classes still contain only docstrings; the next lecture will introduce more substantial class definitions and methods.
Transcript excerpt
0:03 Interesting. >> Okay. All right. Well, uh, happy Monday. It is week eight and, um, and the rest of the quarter, it it goes by quick because we got this week, this is like a a regular week, but then next week is Thanksgiving week. And so, we'll have class on Monday and then Wednesday I'll teach from Zoom. And you know, some of you if you want to travel and leave on, you know, Friday of this week, that's fine, okay? And then you'll miss class on Monday and then you'll log in on Zoom on Wednesday. You know, you lose a couple participation points. It's not a big deal, okay? Uh go spend time with your
0:51 family. It's good. But uh but anyway, it goes by quick because and then after that, it's week 10, right? after Thanksgiving, it's week 10 and um and you know, we're just cleaning up stuff then and then uh and then you have finals week after that. Okay, so it goes uh it goes by quick. Um anyway, uh this week we are returning back to programming concepts in Python. So the last couple weeks we took a slight departure from base Python. uh we introduced num we added packages like numpy and pandas and mapplot lived and those are all I would say they're almost like core packages they're not they're but they're very very very
1:39 essential as far as uh a lot of our applications uh with python u so so many things will use uh those like numpy and uh and pandas and mplot lib and uh you know you I also adapt things to polers for its lazy evaluation and things like that. But um one thing I want to come back to is object-oriented programming in Python. So uh a lot of languages do
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Miles Chen.