I need someone to explain this code for me please.(i would appreciate it if someone explained the whole code all together) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I need someone to explain this code for me please.(i would appreciate it if someone explained the whole code all together)

class Vector2D: def __init__(self, x, y): self.x = x self.y = y def __add__(self, other): return Vector2D(self.x + other.x, self.y + other.y) first = Vector2D(5, 7) second = Vector2D(3, 9) result = first + second print(result.x) print(result.y) alright, i already understand that in self.x , x is a variable not value (which idk why they didn't clarify.....i took some time to understand it on my own) someone once explained to me that self.full_name = name is like saying "self has a full name and this name is "name"" but what about result.x ? What does the dot mean here. Secondly : I understand that __init__ is a special method/dunder and that self.x would be equal to 5 in case of first and 3 in case of second! ( i am not sure) so who is other.x ?? Apparently other.x is supposed to be 3 but i don't understand how or why. How did python even understand that when we say other.x it means the "second" variable, nothing literally got assigned to the dunder __add__ at all.

10th Aug 2023, 3:54 PM
Intermediate Depression
Intermediate Depression - avatar
8 Answers
0
Intermediate Depression It's because __add__() is a MAGIC method. You understand what does magic mean, right? It's something that is hard to explain how it works.. Magic methods have its own special rules. We don't know how does it work exactly. Well.. The self is always placed at the first of every methods in a class.. So the "first" object in the line result = first+second refers to "self" then second refers to the "other". That's how I understand, I guess? Btw, I updated my previous answer a little bit.
10th Aug 2023, 5:34 PM
Dragon RB
Dragon RB - avatar
+ 1
__init__ is the method constructor for the object. You're required to pass the arguments in order to construct it. Self refers to the current object. __add__ is a method for operator overloading: it allows you to define what adding two objects of this class together means. In this case it means creating a new vector with the values of both added together. The code simply performs such addition and outputs the result of x and y of the newly created vector from the addition operation.
10th Aug 2023, 4:29 PM
Marcos Chamosa Rodríguez
Marcos Chamosa Rodríguez - avatar
+ 1
Okay, lemme explain a little. So, remember this order: Vector2D(x,y) In this case, first.x = 5, and first.y = 7 [first = Vector2D(5,7)] This order applies to the other object of the Vector2D class in the code you provided[second = Vector2D(3,9)]. So, the dot in this case refers to the variable x || y in the class. [result.self.x||y == result.x||y] Now, let's take a look at this line: `def __add__(self, other): return Vector2D(self.x+other.x,self.y+other.y)` The __add__ method in short, is used for adding objects together using the "+" operator. The "other" refers to another object of class Vector2D. [other.self.x||y == other.x||y] `self.x + other.x, self.y + other.y` means that it gets the x from the object and then add it to x from the other object, this also applies to y.. So, the line "result = first+second" works something like: X Y ("first") (5, 7) + + => [__add__] (Vertically) X Y ("second") (3, 9) = = X Y ("result") (8, 16) => result.x = 8; result.y = 16.
10th Aug 2023, 5:05 PM
Dragon RB
Dragon RB - avatar
+ 1
Dragon RB YOU ARE A GENIUS!! " so the "first" object in the line result = first + second refers to "self" then second refers to "other"" THAT'S IT this is where we told python who is gonna be self and who is gonna be other! By the order we wrote them!!!! I edited the code to put your theory onto examination, i overrided the "+" operator to act like "/" operator and then switched first and second and the output CHANGED (because it's division now, so the order matters.) And since __add__() is a Magic method it is called automatically when we use "+" operator and so it got called when we wrote "first + second" and used the order in which we wrote the objects in.❤ THANK YOU SO MUCH❤!!!!.(it has been bugging me for days lol) ( also the term magic method is very new to me i am trying my best to understand Magic methods)
10th Aug 2023, 6:55 PM
Intermediate Depression
Intermediate Depression - avatar
+ 1
Intermediate Depression I'm not a genius. I searched and went a bit deeper in magic method to understand how it works exactly, when I got to the magic method lesson. BTW I'm glad I helped! 👍 (Some other magic method names beside __add__: __sub__ for -, __mul__ for *, __str__ for string, __floordiv__ for //, __truediv__ for /, and many more. By understanding magic method, it will become handy for performing operations with your own custom objects.)
11th Aug 2023, 1:16 AM
Dragon RB
Dragon RB - avatar
+ 1
Dragon RB Thank you very much!!(also isn't __floordiv__ for //? not __div__!)
11th Aug 2023, 2:11 AM
Intermediate Depression
Intermediate Depression - avatar
+ 1
Intermediate Depression Sorry, I made a mistake. Fixed!
11th Aug 2023, 2:16 AM
Dragon RB
Dragon RB - avatar
0
Dragon RB TY i understand much more now! I am just wondering how did python know that other refeers to "second" and first == self, we never told python that other == second or that self ==first. That's where i am confused .
10th Aug 2023, 5:23 PM
Intermediate Depression
Intermediate Depression - avatar