Traceback (most recent call last):
line 56, in <module>
distanceToOne = point1.Distance(pointUser)
line 22, in Distance
distance = math.sqrt((self.__x - toPoint.x)**2 +(self.__y - toPoint.y)**2 +(self.__z - toPoint.z)**2)
AttributeError: 'Point' object has no attribute 'x'
For some reason I keep getting the above error message whenever I get to: distanceToOne = point1.Distance(pointUser)
after grabbing my three points to calculate the distance from.
Here is a better view if needed: http://pastie.org/private/vige6oaphkwestdemr5uw
Thanks in advance for your help!
import math
class Point(object):
def __init__(self, x = 0, y = 0, z = 0, description = 'TBD'):
self.__x = x
self.__y = y
self.__z = z
self.__description = description
def SetPoint(self, coords):
self.__x = coords[0]
self.__y = coords[1]
self.__z = coords[2]
def GetPoint(self):
return [self.__x, self.__y, self.__z]
PointCoords = property(GetPoint, SetPoint)
def Distance(self, toPoint):
toPoint.PointCoords[0]
toPoint.PointCoords[1]
toPoint.PointCoords[2]
return math.sqrt(
(self.__x - toPoint.x)**2 +
(self.__y - toPoint.y)**2 +
(self.__z - toPoint.z)**2)
def SetDescription(self, description):
self.__description = description
def GetDescription(self):
return self.__description
PointDescription = property(GetDescription, SetDescription)
if __name__ == "__main__":
print "Program 9: Demonstrate how to define a class"
point2 = Point()
point1 = Point(10, 54, 788, 'Ploto')
point2.PointCoords = 77, 2, 205
point2.PointDescription = 'Mars'
doAnother = "y"
while(doAnother == "y"):
pointX = raw_input("Enter a X Number: ")
pointY = raw_input("Enter a Y Number: ")
pointZ = raw_input("Enter a Z Number: ")
# Constructor - Represent the user's location
pointUser = Point(pointX, pointY, pointZ, 'Sun')
distanceToOne = point1.Distance(pointUser)
distanceToTwo = point2.Distance(pointUser)
# Comparing the two distances between the two to see which one is the closest
if (distanceToOne > distanceToTwo):
closest = point2
else:
closest = point1
print ('You are closest to',closest.PointDescription(), 'which is located at ',closest.PointCoords())
doAnother = raw_input("Do another (y/n)? ").lower()
print ('Good Bye!')
The actual error is due to accessing toPoint.x
, which doesn't exist because you have never defined it.
On a related note, prepending attributes with double underscores activates pythons name mangling feature. The actual attributes will still be publicly accessible at my_point._Point__x
, my_point._Point__y
, etc from outside the class.
As a matter of style, there does not seem to be any reason to use name mangling in this case. The intended use-case of this feature is to avoid clashes with inherited classes, it's not really about trying to make "private" variables (for that, the convention is to use a single underscore to indicate when an attribute is implementation detail).
In your case, I think you should just name (and access) the attributes normally x
, y
, etc. In python we don't normally write getters and setters for class members unless there is a special requirement to do so, because Python is not Java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With