Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginer oop help
#1
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)
Reply
#2
Do you want the addition to apply to one of the objects or make a new object ?
Reply
#3
(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:
Reply
#4
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
Reply
#5
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
Reply
#6
The previous code I posted has been edited to show returning a new object as well.
see Editing and deleting threads/posts
Reply
#7
(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]: 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
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.)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Please help to the beginer polo 4 2,895 Mar-27-2019, 01:22 AM
Last Post: polo
  Python for beginer triluan 2 2,378 Mar-03-2019, 12:40 PM
Last Post: triluan
  Need Help with Start-up (DIY Beginer) Styles4gh 2 2,465 Mar-09-2018, 02:06 AM
Last Post: Larz60+
  help for beginer libed 3 4,609 Nov-11-2016, 04:34 AM
Last Post: Blue Dog

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020