Operator overloading - Intermediate Python Programming p.19
Key Takeaways
Detects collisions between two blobs in Python using the '+' operator
Full Transcript
what's going on everybody Welcome To Part 19 of our intermediate python Series in this video and the kind of coming videos uh we're going to be talking about basically one very broadly how everything in Python is objectoriented so whether or not you're writing object-oriented programming uh code whether or not you're writing a class or whatever everything you're doing is an object and and you're dealing with objects um but more specifically in in this one and for sure the next tutorial we're going to be talking about the a special method or magic method and even more specifically operator overloading so uh this is the code that we're uh starting with it's basically the same thing that we've been building up to this point although I did make a couple of changes um I'll go over them in a moment but I'm just going to say where you can get the code you can go to pythonprogramming.net um come down to the fundamentals here and then in the intermediate python series you can click on that come down to part 18 it'll be after de or 19 rather it'll be after decorators it's not up yet because I haven't done it I'm filming it so anyway it'll be there though and you can click that and you can copy and paste the code if you want or you can make the changes um they're very subtle and small really all I did majorly was uh add green blob so add a a class for green blob it inherits from blob which we already discussed um but the other thing I did was remove passing of color that was kind of of silly to pass a color to the the blue blob class it should just be a the color blue all the time so that's kind of stupid um so what I've done here is just hardcoded that color again you could also pass a constant um I just chose not to for inheritance sake just in case you wanted to import this or something um anyway yeah when we pass it here we're not passing a color so I made those small changes and now with green blob we are calling green blob here uh we can run it and we get the blobs as expected cool now what I want to do is begin adding um interaction between these blobs anyway um and what I'd like to be able to do is say okay when a a GRE a blue and a red blob come into contact wouldn't it be nice if we could just say let's say you've got a blue blob that's a blue blob object variable name for now um and then you've got a red blob object wouldn't it be cool if we could just say like plus right cuz that's what's really happening they're combining they're they're banging into each other we want to be able to use plus okay so that's what we're going to do and when we do that uh we're going to have to define a a method that will handle that that that sum that add operation and to do that we're going to use that's going to be that's a form of operator overloading you can overload all of the operators that you could possibly use um assignment operators comparison operators and so on okay you can overload all of them and you can also overload obviously the plus the minus the multiplication and division all of them you can overload all of them today we're just going to do the plus sign and what's cool is as you'll see the way that we do it is it's it'll be unlike any other method um it's just it's just it's cool so anyways let's get started so to do it it's a magic method a special method it's Dunder ad so Define Dunder ad as always we have to P self but when you have an add operation you're adding you know you've got one thing plus something else it'll be that second parameter whatever that is that in our case should be another blob right we're going to add blobs together in theory it could be something else um but for now let's just say it's a blob great so now basically it could be any of a hand full of blobs right right it could be a red blob a green blob or a blue blob so we need a handle for each of those and also and in case it's none of those um because maybe someone doesn't understand how to properly use this um this method so so let's do that so let's say um if other blob. color equals and let's say let's start if it's a red a red blob so 255 0 0 if it is that we're going to say self. size minus equals other blob. size and then we're going to say other blob. size minus equals um self. size so in this case self is the blue blob because this is pertaining to the blue blob class we're not going to add an ad method or um to any of the other classes it's unnecessary we don't need to um eventually you might um but the the protagonist of our blob story is just the blue blob like that's the main character okay so that's really who we care about the other ones you could say you might have a scenario where the green blobs need to be able to be added to Red blobs and we need to figure out how to handle that um we're just not going to handle that because we're just going to say green blobs don't interact with red blobs they only interact with blue blobs and red blobs don't interact with green blobs and so on it's just interaction with the blue blob so in this case just to be absolutely clear um it's not really specific to this method but when you do something like this like keep in mind that self. size is being modified in place here well not totally in place but sort of um and once you've done that then we're subtracting size so we're not subtracting the original size of that blob when they came into contact we're actually subtracting that size after we've subtracted raed the size of the red blob so really if the red blob is larger than the blue blob lights out for the blue blob and he's not really going to do any damage to the red blob so keep that in mind um and in fact it it might it in the it could even add to the size of the red blob anyway you want to think about these things we're trying to make just a really simple method here um so I'm going to keep it the way it is but just know this might not actually be what you intend to do so pay attention to little subtleties like that okay so anyway L if other blob. color and in this case what if it's green well if it's green um we're going to say self. size plus equals other blob. size no problem and then we're going to say other blob. size equals z that's it for green now we need to handle if it's a blue blob so we might say other blob. color equals and then 0 0 2 5 if this is the case for now we're going to pass we're just going to say right now blue blobs don't do anything with each other now if none of these are hit someone obviously passes a color that isn't one of these three colors so if that's the case we're going to say else and we're going to raise an exception and the exception is going to say uh try to combine one or multiple Blobs of unsupported colors okay so that is our Dunder ad method and now we're ready to add blobs we can already do it but now we actually need to have some sort of logic that is going to actually add these blobs but you you could you could add them so um let me see where are we actually so we created we're creating them here um I just want to real quickly show you a blob getting we're I'm just going to comment this out you can either follow this or just watch if you want uh so we've got green blobs and blue blobs so now what we can say is blue blobs and their IDs will be um or their keys are just numbers so we'll just say blue blob zero plus uh let's do blue blob zero plus red blobs zero so we're going to say they boom they both come into contact First we're going to say as well um blue blob size red [Music] size format and then let's take this blue blobs this is supposed to be red we'll change that um and also it needs to be uh do size blue blob zero and then red blobs zero do size and then we'll just copy paste and we should see a change right so uh this is black just because we' never displayed anything but here is whoops here is the original information the blue was a six the red is a seven it was subtract seven was subtracted from six boom you got negative 1 and red ended up gaining a size because of what we actually caught before so if blue is a negative size you subtract a negative that's adding so it actually got a little bigger so um it consumed blue a little bit so um anyway that's how it works we can see it worked on those on the um objects but we need to come up with code that will logically do this rather than just doing it as we explicitly call it so we actually need to come up with code that will uh detect when two blobs have collided so that's what we're going to be doing in the next tutorial is actually detecting that Collision um if you have any questions comments concerns whatever up to this point feel free to leave them below otherwise I'll see you in the next tutorial
Original Description
Welcome to part 19 of the intermediate Python programming tutorial series. In this tutorial, we are going to introduce the "special" or "magic" methods of Python, and, with that, talk about operator overloading, which is where we can define new ways for Python to handle operators like "+" in our example.
https://pythonprogramming.net
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
🎓
Tutor Explanation
DeepCamp AI