I'm fairly new to Python and I'm working with classes(in Python) for the first time. When I try to use the constructor, I get an AttributeError, though I'm not really sure why this is occurring.
class Pawn(object):
def __init__(self, x: int, y: int):
if (not x in range(1,9)):
raise ValueError('Invalid X-Position')
if (not y in range(1,9)):
raise ValueError('Invalid Y-Position')
self.x = x
self.y = y
@staticmethod
def parseFromString(sPawn):
try:
return Pawn(ord(sPawn[0]) - 96, int(sPawn[1]))
except ValueError:
raise ValueError('Parse Failed. Please check pawn string.')
def getLethals(self):
lethals = {}
try:
lethals.append(Pawn(self.x - 1, self.y))
lethals.append(Pawn(self.x + 1, self.y))
lethals.append(Pawn(self.x, self.y - 1))
lethals.append(Pawn(self.x, self.y + 1))
lethals.append(Pawn(self.x - 1, self.y - 1))
lethals.append(Pawn(self.x + 1, self.y + 1))
lethals.append(Pawn(self.x - 1, self.y + 1))
lethals.append(Pawn(self.x + 1, self.y - 1))
except ValueError:
pass
return lethals
def packToString(self):
return str(chr(self.x + 96)) + str(self.y)
@property
def x(self):
return self.x
@property
def y(self):
return self.y
When I try to create an instance:
def testFunc():
pawn = Pawn(2,4)
I get an:
AttributeError: can't set attribute
Tracing all the way back to 'self.x = x' of the constructor of 'Pawn'.
EDIT: I didn't understand that properties revoked writing privileges of class variables. I will add setters.