CS 238 Week 5 Lecture
Overview
James Andro-Vasko demonstrates Python polymorphism by storing `Cube` and `Square` objects together in a list or dictionary and calling methods on each object without checking its type. The examples show runtime method dispatch, inherited methods, and how missing methods cause `AttributeError`; defining a placeholder such as `pass` or returning `0` avoids the error, though method behavior should still make sense for each shape.
Key takeaways
- Python can hold different object types in one list or dictionary and resolve a method call at runtime based on the object's class.
- In the example, `Cube(7)`, `Square(4)`, and `Cube(3)` share a list, and calling `get_area()` invokes the appropriate implementation for each object.
- A method inherited from `Square` makes `get_perimeter()` callable on `Cube`, but being callable does not guarantee that the method's result is semantically appropriate for a cube.
- A mixed collection raises an error if an object lacks the method being called; every object must provide that method directly or through inheritance.
- Defining a placeholder with `pass` prevents a missing-method error but returns `None`; returning `0` is an alternative when that value is suitable.
- The same polymorphic dispatch pattern works with dictionary values by iterating through `shapes.items()` and calling methods on each value.
Chapters
- A `shapes` list holds a `Cube(7)`, a `Square(4)`, and a `Cube(3)` together.
- A `for shape in shapes` loop calls `get_area()` on each object, and Python dispatches to the implementation for that object's class.
- The loop demonstrates polymorphism: the variable `shape` refers to different object types across iterations.
- Calling `get_perimeter()` works for `Cube` because it inherits the method from `Square`, though the inherited square perimeter may not be meaningful for a cube.
- Calling `get_volume()` fails on a `Square` because that class has no such method; the mixed-object loop raises an error when it reaches the square.
- Adding `get_volume()` to `Square` with `pass` prevents the missing-method error but returns `None`; returning `0` is another proposed placeholder.
- A dictionary stores `Cube` and `Square` instances under keys such as `cube1`, `square1`, and `cube2`.
- Iterating with `shapes.items()` provides each key and object, allowing calls such as `value.get_area()` to dispatch to the correct class method.
- The same method-availability requirement applies to dictionary values as to list elements; runtime type checks are unnecessary when objects support the called methods.
Transcript excerpt
0:00 Hello CS 238 class. Uh for this week we're going to be covering this topic uh polymorphism. I do have the notes posted that goes over all these and I'll just go over an example here uh referring back to the square and cube examples from last week. So in Python uh polymorphism is a very simple structure to implement compared to um other object-oriented languages like C++ and Java and C. which is definitely much more simpler in Python cuz Python is very dynamic uh language. So u pretty much what we can do to polymorphism is we can have either a list or a dictionary and in CS138 or in any of your earlier programming classes you always had a list or a dictionary where
0:49 every element was the same element. So we had like a list dictionary of a key that maps to the same values or even for one of our earlier assignments assignment two we had sort of this uh dictionary or we could have had a list of products but now we can actually can have a list of all different objects. So we can have let's say we have a square and cube uh class defined right here. We can have one single list or one single dictionary that kind of mixes up and have has all kinds of objects. sum square, sum cube and so forth. And we'll go over some of the potential um challenges and issues that could arise uh with implementing this u topic of uh polymorphism. So you know I guess polymorphism so poly and morph. So poly is multiple forms
1:37 morph I guess it means you can have multiple forms I suppose. So let's go ahead and I'm going to first just create maybe a list of different objects for
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
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, James Andro-Vasko.