Save this video free

CS 238 Week 6 Lecture

James Andro-Vasko · 20:32 · Watch on YouTube

Part of University of Nevada, Las Vegas CS 238. See all 9 lectures and search them

These notes were made with YouTube Collector. Paste any YouTube link and get the same: a summary, chapters and a searchable transcript.

Overview

James Andro-Vasko explains how Python operator overloading lets a custom `Scale` class use `+`, `-`, `*`, and `/` to combine its weight values. He emphasizes returning a new `Scale` object rather than mutating either operand, demonstrates the corresponding special methods, and cautions that overloaded operators should only be used when their meaning is intuitive.

Key takeaways

Chapters

0:00 Operator Overloading and the Non-Mutation Principle
3:00 Adding Scale Objects with Python’s `__add__`
10:00 Implementing Scale Subtraction, Multiplication, and Division
15:00 True Division, Floor Division, and Choosing Operators Carefully

Keep these chapters and the full searchable transcript in your own library.

Transcript excerpt

0:00 Hello CS238 class. Uh for this week we'll be covering operator overloading. And the way the idea of operator overloading is that we have a custom type. In this case, I'll use a class called scale. Um and way it works is so you know for types like integers and uh decimals, you can use the um plus operator, the star, the division slash operator to perform several sets of calculations using these you know operators you have in the language. So, at this point those operators are only usable for these primitive through a built-in types. But in uh Python or any lot of other languages as well, you can overload, you can implement um these operator

0:49 functions like star, plus, you know, the minus uh sign for custom types, for types that are not uh built into the language. So, let's go ahead and now discuss how we can overload um custom operators for this uh scale type uh defined. Okay, so before we go into details of the scale class, so consider the following case right here. So, let's say for right now we have like variables let's say x equals 5, y equals 8, and then z or zed equals 10. And let's say we decide to do something like z or zed equals x * y. I guess me let's just do addition first,

1:36 like this. So, um this is a fairly uh familiar statement to have. So, this um line of code what it will do will take the value of x, add it to value of y. So, x contains the value 5, y is 8, we get 13 as a result, and we're going to store the value 13 into z or Z. And if you were to print X and Y, it

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.

Want the full transcript?

Save this video in YouTube Collector to get its complete searchable transcript, your own AI summaries, and a library that keeps every video you collect in one place.