Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginer oop help
#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


Messages In This Thread
beginer oop help - by noissue - Oct-11-2016, 06:17 PM
RE: beginer oop help - by Yoriz - Oct-11-2016, 06:20 PM
RE: beginer oop help - by noissue - Oct-11-2016, 06:22 PM
RE: beginer oop help - by Yoriz - Oct-11-2016, 06:33 PM
RE: beginer oop help - by noissue - Oct-11-2016, 06:43 PM
RE: beginer oop help - by wavic - Oct-11-2016, 07:10 PM
RE: beginer oop help - by Yoriz - Oct-11-2016, 06:50 PM
RE: beginer oop help - by micseydel - Oct-12-2016, 03:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Please help to the beginer polo 4 3,903 Mar-27-2019, 01:22 AM
Last Post: polo
  Python for beginer triluan 2 3,141 Mar-03-2019, 12:40 PM
Last Post: triluan
  Need Help with Start-up (DIY Beginer) Styles4gh 2 3,242 Mar-09-2018, 02:06 AM
Last Post: Larz60+
  help for beginer libed 3 5,523 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