CS 238 Week 2 Lecture
Watch on YouTube →
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
0:00
Why Python Classes Encapsulate Reusable Data Types
- 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.
1:27
Defining Clock and Adding an Instance Field
- 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.
5:04
Initializing Clock Fields with __init__
- 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.
8:26
Parameterized Constructors and Default Clock Values
- 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.
16:05
Double-Underscore Fields and Getter Methods
- 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.
22:34
Setter Methods Normalize Clock Values
- 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.
33:13
Assigning Clock Variables Creates an Alias
- 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.
35:24
Passing a Clock Instance to a Standalone Function
- 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.
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.