CS 238 Week 3 Lecture
Overview
James Andro-Vasko demonstrates how to place a Python `Clock` class in `clock_util.py` and reuse it from separate main programs, comparing module imports, aliases, and selective imports. He then shows how lists and dictionaries can store `Clock` objects, iterate over them, and invoke methods such as getters and setters through each retrieved object.
Key takeaways
- A Python module named `clock_util.py` is imported using `clock_util` without the `.py` extension; its class is then available as `clock_util.Clock`.
- Importing a module with an alias, such as `import clock_util as clu`, keeps references concise while retaining the module namespace.
- Selective imports such as `from clock_util import Clock` remove the module prefix, while wildcard imports risk collisions with names already defined in the importing file.
- Lists and dictionaries can store references to custom `Clock` objects, allowing each retrieved element to use class methods such as getters and setters.
- A list of clocks can be traversed with either indexed access (`clocks[i]`) or direct iteration (`for c in clocks`); dictionary entries are retrieved by keys instead.
- Python dictionary values can be traversed with `.values()`, keys with `.keys()`, and entries are iterated in insertion order.
Chapters
- Keep `clock_util.py` and `main.py` in the same directory for a straightforward custom-module import.
- With `import clock_util`, access the class as `clock_util.Clock` and construct an object with values such as 20, 45, and 16.
- Separating the class into a module lets multiple Python programs reuse it instead of duplicating the class code.
- Use `import clock_util as clu` to shorten repeated references, such as `clu.Clock`.
- Use `from clock_util import Clock` to refer to `Clock` directly without the module prefix.
- Wildcard imports can bring every module attribute into the current file, but may overwrite existing names and create conflicts.
- Initialize a `clocks` list with `Clock` instances or start with an empty list and add objects using `append()`.
- A numeric loop can access each object with `clocks[i]`; a direct loop such as `for c in clocks` visits each clock in turn.
- List elements are `Clock` objects, so calls such as `get_hr()`, `get_min()`, and `get_seconds()` retrieve their fields.
- Use an indexed object to update it, for example `clocks[2].set_min(56)`.
- The `clocks` dictionary maps string keys such as `grandfather`, `digital`, and `iPhone` to `Clock` objects.
- Retrieve an object with `clocks['digital']`, then call its methods; for example, `clocks['digital'].set_hr(16)` updates its hour.
- Iterate over `clocks.values()` to process clock objects or over `clocks.keys()` to process their labels.
- Python dictionaries preserve insertion order when iterating, and dictionary values can be custom objects rather than primitive types.
Transcript excerpt
0:00 Hey everybody. Uh welcome to uh week three uh lecture video for the CS 238 class. For this um video, we're going to just essentially go over um similar content from last week. So we'll go over this uh clock type again, but in this video we're going to cover how to um import our custom classes and modules into other uh files. And also we will go over maintaining a list and dictionary of custom types. So we can have a dictionary or a list of um clock objects. Let's go ahead and uh begin talking about that. Okay. So before I go into this um
0:48 importing I want to first just mentioned I created a clock util. py uh file and the purpose is so it's not going to create any uh possible confusion when we import anything. So that's our first step. So um pretty much you've probably done importing in your CS138 class already. You would import pandas or um numpy. my own personal um content I did import in the past sci and not and mapplot lib um in the past and of course all kinds of you know python modules you can import so right now we're focusing on importing our custom module so right now our clock util py that's going to be our custom module and within this module
1:36 we have only one attribute this uh class clock defined within it so to um import this into my main I would just use import clock util. So since our file is called clock util. py then clock util without the py without the py that's going to be our module name. And also make sure that you have your module and your main in the same directory. So if I ls here on Linux
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.