Dec-09-2017, 09:12 PM
I've created a skeleton Point class for points in a 3D space (e.g. with x, y, and z co-ordinates). I want the co-ordinates to be integers and for any other input value to be refused. That part works fine. I just can't figure out a way to have the class constructor refuse to initialize an object if any of the three inital values is not an integer. How do I do that? Here's my code so far:
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
@property
def x(self):
return self.__x
@x.setter
def x(self, a):
if isinstance(a, int):
self.__x = a
else:
print("x must be an integer")
return None
@property
def y(self):
return self.__y
@y.setter
def y(self, b):
if isinstance(b, int):
self.__y = b
else:
print("y must be an integer")
return None
@property
def z(self):
return self.__z
@z.setter
def z(self, c):
if isinstance(c, int):
self.__z = c
else:
print("z must be an integer")
return None
