0

so I tried making a little program using classes. I have two classes, the first one is this:

class Point():
 def __init__(self, x, y, z):
    self.__x = x
    self.__y = y
    self.__z = z

 def get_x(self):
   return self.__x

 def get_y(self):
   return self.__y

 def get_z(self):
   return self.__z

Basically this class is about a point in the 3D Space. Now this next class, I made functions in it but I don't know how to start. How do I call the point's coordinates from the Class "Point" above? Is it something like "self.__points[i].get_x()"?

class Parcel:

 def __init__(self, points):

   self.__points = points

 def get_area(self):
   pass
 def get_perimeter(self):    
   pass
 def get_mean_elevation(self):
   pass

p1 = Point(0,0,10)
p2 = Point(1,1,20)
p3 = Point(2,0,15)


points = [p1, p2, p3]


p = Parcel(points)
print p.get_area()
print p.get_perimeter()
print p.get_mean_elevation()

I haven't done the three functions(get_area and get_perimeter, get_mean_elevation) because I don't know how to summon the coordinates of the points from the list. When I use the hint given to us "self.__points[i].get_x()" to get the coordinate X from the point it doesn't work.

2
  • 1
    Don't use getters when you can just access the attribute directly. There is no need to make your attributes private here. Commented Mar 19, 2015 at 11:59
  • 1
    If you don't want your attributes to be mutated, perhaps a collections.namedtuple() class is in order. Commented Mar 19, 2015 at 11:59

1 Answer 1

1

self.__points is a list, so you'd have to iterate over that list to get to each of the points, or use subscription to get to specific points:

for point in self.__points:
    x = point.get_x()
    # do something with this x coordinate

or

first_point = self.__points[0]

Note that in Python, there is no need to use getters and setters, not should you be using double_underscores on your attribute names. Double underscores are meant to limit the chances that a subclass is going to re-use the same attribute name, not to make attributes private.

For a Point, I'd use collections.namedtuple() to produce a class that cannot be mutated after setting the attributes:

from collections import namedtuple

Point = namedtuple('Point', 'x y z')

class Parcel(object):
   def __init__(self, *points):
       self.points = points

   def get_area(self):
       pass

   def get_perimeter(self):    
       pass

   def get_mean_elevation(self):
       pass

p1 = Point(0, 0, 10)
p2 = Point(1, 1, 20)
p3 = Point(2, 0, 15)

p = Parcel(p1, p2, p3)

In your methods, you can then use self.points and each point has the attributes x, y and z, but you can also use indexing to the to the 3 coordinates of the point:

>>> p1 = Point(0, 0, 10)
>>> p1
Point(x=0, y=0, z=10)
>>> p1.z
10
p1[2]
10

Because Point is a subclass of tuple, the attributes are immutable:

>>> p1.z = 42
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, I have done it with the first method and have done the get_mean_elevation function. I have trouble incorporating this two functions(get_area and get_perimeter) with the suggested method of getting the needed coordinates. I've edited the code now.
@MichaelHander: I'd be better if you made that a new question; you changed your question considerably. The Q&A format is not suitable for an ongoing conversation where you change your code and I change my answer in a feedback loop.
@MichaelHander: As such I rolled back your change. Your x1, y1 = points[0] code won't work unless points[0] is a sequence of exactly 2 elements. If Point is a namedtuple like in my answer, then it would be a sequence, but you are working with points in 3D space, giving you 3 elements in the sequence (x, y, z), not 2.
@MichaelHander: in your case Point is not a sequence, so you need to use x1, y1, z1 = points[0].get_x(), points[0].get_y(), points[0].get_z() to extract the three coordinates by explicitly calling your getters. Then you need to figure out how to calculate the area, etc. for a 3-dimensional polygon, your code is for 2-dimensional geography instead.
Yeah you are correct, I was wondering why our instructor needs us to find the 2d area and 2d perimeter when it has 3 coordinates so I just followed the directions. He just said that we just don't mind z coordinate. By the way I have done the get_area function now. Thank you very much.