Python Forum
beginer oop help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: beginer oop help (/thread-439.html)



beginer oop help - noissue - Oct-11-2016

im beginer to oop and i have problem with understanding:
how to do a method that adds 2 objects with x and y coordinates
do i have to make another class to add them ?
or i can make method inside class? im lost
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

#main
objectA = Point(3,5)
objectB = Point(7,5)



RE: beginer oop help - Yoriz - Oct-11-2016

Do you want the addition to apply to one of the objects or make a new object ?


RE: beginer oop help - noissue - Oct-11-2016

(Oct-11-2016, 06:20 PM)Yoriz Wrote: Do you want the addition to apply to one of the objects or make a new object ?

i would like to know answer to both please :huh:


RE: beginer oop help - Yoriz - Oct-11-2016

Heres a couple of methods adding one point to another and creating a new point from adding points.
class Point(object):
   def __init__(self, x, y):
       self.x = x
       self.y = y

   def __repr__(self):
       return 'Point> x: {}, y: {}'.format(self.x, self.y)

   def add_points_to_return_a_new_point(self, point):
       return Point(self.x + point.x, self.y + point.y)
       
   def __add__(self, point):
       self.x += point.x
       self.y += point.y

#main
objectA = Point(3,5)
objectB = Point(7,5)
print(objectA)
objectA + objectB
print(objectA)
objectC = objectA.add_points_to_return_a_new_point(objectB)
print(objectC)
Output:
Point> x: 3, y: 5 Point> x: 10, y: 10 Point> x: 17, y: 15



RE: beginer oop help - noissue - Oct-11-2016

Wow i forget about that magic methods, so this is how it works.
and here
(Oct-11-2016, 06:33 PM)Yoriz Wrote: objectA + objectB
so you can just add two objects
but what if there was a lets say text ?

There should be some edit button on this forum


RE: beginer oop help - Yoriz - Oct-11-2016

The previous code I posted has been edited to show returning a new object as well.
see Editing and deleting threads/posts


RE: beginer oop help - wavic - Oct-11-2016

(Oct-11-2016, 06:43 PM)noissue Wrote: Wow i forget about that magic methods, so this is how it works.
and here
(Oct-11-2016, 06:33 PM)Yoriz Wrote: objectA + objectB
so you can just add two objects
but what if there was a lets say text ?

There should be some edit button on this forum

For the string object __add__ dunder method is defined to do just that. Add B string to the end of A string. 

In [1]: A = 'cats'

In [2]: B = 'dogs'

In [3]: print("It's raining " + A + " and " + B + "\u2614")
It's raining cats and dogs☔

In [4]: 



RE: beginer oop help - micseydel - Oct-12-2016

I'd like to suggest some improvements to Yoriz's solution to make it more Pythonic, using __iadd__
class Point(object):
   def __init__(self, x, y):
       self.x = x
       self.y = y

   def __repr__(self):
       return 'Point> x: {}, y: {}'.format(self.x, self.y)

   def __add__(self, other):
       return Point(self.x + other.x, self.y + other.y)

   def __iadd__(self, other):
       self.x += other.x
       self.y += other.y
       return self

objectA = Point(3,5)
objectB = Point(7,5)
print(objectA)
print(objectA + objectB)
print(objectA)
objectA += objectB
print(objectA)
Output:
Point> x: 3, y: 5 Point> x: 10, y: 10 Point> x: 3, y: 5 Point> x: 10, y: 10
When I see the plus (+) operator (without it being in +=) I don't expect a side effect. I would not expect one. So I have __add__ instead return a new value. If we really want to modify a Point (which arguably, shouldn't be done) then Python gives you += for free, as it will do the regular plus then replace the left hand side with that new object. __iadd__ is simply more efficient, if you want to make it that way.

As an aside - I believe immutability is a wonderful thing, and I might even use __iadd__ in this case to throw an exception to enforce it. Immutable code is much more easily reasoned about, since one need not worry about side effects, just read through straightforward code. (Note: in this case, creating a new point is cheap enough and += clear enough that the negatives to throwing an exception probably aren't worth it in practice even if the principal holds.)