Python OOP Tutorial (Object Orientated Programming ) - Overriding Methods

preview_player
Показать описание
Classes and objects python tutorial #4. This video covers overloading methods such as __gt__, __len__, __eq__ and other python builtins to allow for operations and comparisons on your created objects. This is very cool and very useful!

**************************************************************

**************************************************************

Please leave a LIKE and SUBSCRIBE for more content!

Tags:
- Tech With Tim
- Python Tutorials
- Object and Classes Tutorial
- Objects and Classes Python
- OOP Python
- Object orientated python
Рекомендации по теме
Комментарии
Автор

I notice a lot of people seem to be confused with how these functions seem to be triggered without calling them. Remember these are built in functions that have already been defined. By redefining each of the functions Tim has showed in this video, when the addition, subtraction, multiplication etc is used within this program the redefined or in other words "overloaded" functions like __add__, __sub__ etc will be used when the corresponding arithmetic operators are used like +, -, * etc.

Now for strings, the "+" is used to concatenate two strings together. (Just to clear that up for those that asked this question)

mejiab
Автор

8:58 for people who didn't understand, it is the Pythagorean theorem; a² + b² = c²

sciencedoneright
Автор

Leaving this here for anyone who sees this in the future:


I *think* there's a mistake toward the end of the video, when Tim quickly says that he could have used __len__ instead of the length() method that he made. I'm not sure if I'm understanding 100% correctly, but I don't think this makes sense. __len__ looks for the number of items (attributes) in an object. In this case, every Point() object has two attributes. The length() method, on the other hand, is literally a mathematical formula for determining the distance of a point from the origin 0, 0. These are two completely different things.

For example, if we have Point(1, 2), the __len__ of that is 2, because there are two attributes (x and y). However, the *length*, which is a numerical value as determined by that formula, would be sqrt(1**2 + 2**2), which is about 2.23. This is why some of you are getting errors about float types not being integers. __len__ is trying to find the integer value of how many attributes are in the object, whereas length() is an actual float number derived from a calculation.

Other than that this is rock solid, all of Tim's videos are amazing. I have learned more about OOP in a couple hours watching these than I did taking a Python Bootcamp course from Udemy. Tim, if you ever read this, you seriously rock. You are going to go far in the CS world.

rrl
Автор

As a beginner, this tutorial has been hard for me. but thanks Tim.. i'm learning!

branded
Автор

1:21 Tim: "you'll see what i mean in just a second". 5 hours later, me: "this is a very long second..." Anyways bro, my ineptitude not yours. You're the best out there thanks a million for all your help!

xarcu
Автор

tim you are so good at doing these YT tutorials.

damonchase
Автор

I think you're wrong about your def __eq__. They can still have the same length if their x or y coordinates are the same. For example: if they were like this: (3, 2) and (2, 3). Still great videos and good explanation, though.

gerjenvd
Автор

10:56
The function it's called
dir(class)

cristinocanga
Автор

Instead of using built in function, I did used print(p1.__div__(p2)) to do division function. It did result same as the built in function, just to show there is another method to do the operation if the class had these methods :)

endaoyuan
Автор

This is good!
Truly Professional!
Thanks Tim

theperkyfellow
Автор

The equals method is wrong because two points can have the same length without being the same coordinate. For example (5, 5) and (-5, -5), they have the same length but they are two different coordinates

halamadrid
Автор

It might not be in then but you can now use @dataclass which pretty much initializes everything so you dont have to type it out.

jainzy
Автор

at the end of the video, if len(p1) doesn't work for you try print(p1.__len__())
It should work

nialyavuzturk
Автор

I disagree with the way you defined the "equals" method. The other comparators were comparing the scalar lengths (magnitude), while the "equals" method seem to be defined to compare identities? For example, length(2, 2) == length(-2, -2), but clearly they are not the same point. The method as defined in your video would be more suited to an "is" method or something else. This is an identity vs equality issue.

djfoo
Автор

Initially he is showing operator overload

rojadsorum
Автор

overloading methods like: _add__, __sub_, etc.
so in this example where are overriding methods?
another question is if we can create new operators that do not exist in python with these methods?

alloyou
Автор

does __str__ method always used for any type of printing integers?

vipdeveloper
Автор

It's been so difficult for beginners like me...😮😮

RS-eoft
Автор

i know i'm late but why did you add the plus(+) signs in the def__str__(self):
return '('+ str(self.x) +', ' +str(self.y) + ')'

alfredmusiimentamarvin
Автор

why is this bit of code in the initialisation: self.coords = (self.x, self.y)??

kunalsaha
welcome to shbcf.ru