CS 238 Week 2 Lecture
Overview
James Andro-Vasko introduces object-oriented Python by building a Clock class with an __init__ constructor, instance fields, getters, setters, and a standalone function that accepts a Clock object. The examples explain how self identifies the instance, how setters can normalize hours and minutes with modulo arithmetic, and how assigning one variable to another makes both names refer to the same object.
Key takeaways
- A Python class defines a custom data type, and each call to Clock() creates an instance whose fields can hold that object's state.
- The __init__ method initializes instance fields automatically; default arguments let one constructor support both Clock() and Clock(hours, minutes, seconds).
- In an instance method, self refers to the object used to call the method, so c.set_hr(11) updates c while c2.set_hr(11) updates c2.
- Getters and setters create an interface for accessing and changing fields; setters can enforce rules such as hour modulo 24 and minute or second modulo 60.
- Python double-underscore attributes are name-mangled, not strictly private, so they discourage direct access but do not guarantee data privacy.
- The assignment c2 = c makes c2 and c refer to the same object; it does not copy the Clock instance or its fields.
Chapters
- James Andro-Vasko frames classes as custom data types that hide implementation details behind a usable interface.
- He compares Python classes with C++ vectors, maps, and strings, and with classes used inside NumPy.
- He uses Atom on Windows 11 with a WSL Linux terminal and a Python virtual environment for the coding examples.
- The initial Clock class uses Python's class keyword and pass to define an empty class.
- Creating c = Clock() makes c an instance; assigning c.hr = 7 adds an attribute dynamically.
- The example shows that Python permits attributes to be added outside the class, though Andro-Vasko calls this a poor design for the finished class.
- The Clock constructor defines self.hr, self.minutes, and self.seconds and initializes each field to 0.
- Calling Clock() automatically runs __init__, so the resulting instance starts with all three values initialized.
- Andro-Vasko recommends defining a constructor to ensure instance fields are initialized consistently.
- A parameterized __init__ accepts hours, minutes, and seconds, then stores them in the new instance's fields.
- Clock(19, 23, 18) initializes a clock to 19:23:18, while Clock(21, 12, 5) creates a separate instance.
- Default arguments of 0 allow both Clock() and Clock(hours, minutes, seconds); Python type annotations such as int are optional.
- The class marks fields with double-underscore names, such as __hr, and adds get_hr, get_min, and get_seconds methods.
- Calling c.get_hr() passes c as self automatically, so the method can return that instance's hour field.
- Python's double-underscore naming triggers name mangling rather than providing strict privacy; getters provide an interface for reading fields.
- set_hr, set_minutes, and the seconds setter update fields through methods instead of direct assignments.
- The examples use modulo 24 for hours and modulo 60 for minutes and seconds, so setting 30 hours produces 6 and setting 66 minutes produces 6.
- Each setter uses self to modify whichever Clock instance called it, allowing separate objects such as c and c2 to be changed independently.
- After c2 = c, both names refer to the same Clock instance; this is reference assignment, not a shallow copy.
- Changing c's hour with set_hr(33) applies the modulo-24 rule and makes the hour 9 when accessed through either c or c2.
- Python variables that hold class instances refer to objects, so assignment alone does not create an independent object.
- output_clock is defined outside the Clock class and accepts a Clock instance as its argument, without a self parameter.
- Inside the function, calls such as clk.get_hr() invoke Clock's instance methods and retrieve the passed object's values.
- The final example passes a Clock initialized to 19:23:18 and prints its hour, minutes, and seconds through the getters.
Transcript excerpt
0:00 Hello CS238 class. This is Jimmy Andro Vasco. I will be um instructing this course for the semester and um I have a series of these weekly short videos I will do to cover various topics in this course. So in this CS 238 course, we're going to be doing objectoriented Python. And um I'm more of a C++, C, Java person. In those languages, it's very important to create classes because it allows you to um create data structures and data types which can be used in all different types of applications. And the benefit of it is is that if somebody's using a custom or using a data type, it allows them to use it in their program without really exposing too much of the actual implementation details of the
0:48 object to make it very easy to kind of plug and play in different applications. So C++ with maps, uh, vectors, even string types, it has this sort of given to you and you don't have to really know all the details. As long as you know how to use those custom types, you're okay. And in Python with that numpy, it's a module, but within the module, there are classes within it which really help implement this entire dynamic array structure without really having to have you really mess with the actual inner implementation of that class. So that's sort of the purpose of classes. So let's go ahead and first begin with our simple uh test class we will write here called um clock class.
1:36 Okay, so before I write any code, I'm going to just say I'm using Atom editor to write my Python code. I have um on my Windows 11 machine, I have a WSL terminal. This is basically a Linux terminal I can use on Windows. So 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.