Python 类 和错误
Python classes and errors
现在正在做一些家庭作业,似乎无法弄清楚如何解决这个错误。请注意,我不允许导入任何内容。
我认为我收到的错误是由于我的 __repr__
或 __str__
方法造成的。这是我的代码:
class Point:
def __init__(self, x = 0, y = 0):
"""
The constructor allows the user to set values for x and y. Default values are 0.
"""
self.x = x
self.y = y
def translate(self, s, t):
"""
This method translates point x by s and point y by t.
"""
self.x = self.x + s
self.y = self.y + t
return self.x, self.y
def __str__(self):
"""
This method returns a string representation of the point.
"""
return "({0}, {1})".format(self.x, self.y)
def __repr__(self):
"""
This method returns a string representation of the point.
"""
return "({0}, {1})".format(self.x, self.y)
class SimplePoly:
def __init__(self, *vertices):
"""
The constuctor adds all vertices to a list.
"""
self.vertices = vertices
self.pointlist = [self.vertices]
def translate(self, s, t):
"""
This method translates all points of the polygon by (s,t).
"""
for p in self.pointlist:
p.x + s
p.y + t
print(self.pointlist)
但是当我尝试转换到 self.pointlist 中的每个 Point 对象时,我收到错误:
Traceback (most recent call last):
File "<pyshell#170>", line 1, in <module>
h.translate(1,1)
File "C:/Python34/problem2.py", line 74, in translate
p.x + s
AttributeError: 'tuple' object has no attribute 'x'
顶点是点对象。这是我正在测试的代码:
>>> g = Point(2,3)
>>> g2 = Point(5,2)
>>> g3 = Point(6,7)
>>> h = SimplePoly(g,g2,g3)
>>> h.translate(1,1)
您的 self.pointlist
属性是一个包含 一个 元组的列表:
def __init__(self, *vertices):
# ....
self.vertices = vertices
self.pointlist = [self.vertices]
vertices
在这里始终是一个元组,它又包含您的 Point()
对象。元组从不具有 x
和 y
属性;它本身不是 Point
实例。
你也可以把这里的self.pointlist
换成self.vertices
。如果您需要可变序列,请使用 list()
而不是列表文字:
self.pointlist = list(self.vertices)
它使用元组中的元素创建一个列表。
现在正在做一些家庭作业,似乎无法弄清楚如何解决这个错误。请注意,我不允许导入任何内容。
我认为我收到的错误是由于我的 __repr__
或 __str__
方法造成的。这是我的代码:
class Point:
def __init__(self, x = 0, y = 0):
"""
The constructor allows the user to set values for x and y. Default values are 0.
"""
self.x = x
self.y = y
def translate(self, s, t):
"""
This method translates point x by s and point y by t.
"""
self.x = self.x + s
self.y = self.y + t
return self.x, self.y
def __str__(self):
"""
This method returns a string representation of the point.
"""
return "({0}, {1})".format(self.x, self.y)
def __repr__(self):
"""
This method returns a string representation of the point.
"""
return "({0}, {1})".format(self.x, self.y)
class SimplePoly:
def __init__(self, *vertices):
"""
The constuctor adds all vertices to a list.
"""
self.vertices = vertices
self.pointlist = [self.vertices]
def translate(self, s, t):
"""
This method translates all points of the polygon by (s,t).
"""
for p in self.pointlist:
p.x + s
p.y + t
print(self.pointlist)
但是当我尝试转换到 self.pointlist 中的每个 Point 对象时,我收到错误:
Traceback (most recent call last):
File "<pyshell#170>", line 1, in <module>
h.translate(1,1)
File "C:/Python34/problem2.py", line 74, in translate
p.x + s
AttributeError: 'tuple' object has no attribute 'x'
顶点是点对象。这是我正在测试的代码:
>>> g = Point(2,3)
>>> g2 = Point(5,2)
>>> g3 = Point(6,7)
>>> h = SimplePoly(g,g2,g3)
>>> h.translate(1,1)
您的 self.pointlist
属性是一个包含 一个 元组的列表:
def __init__(self, *vertices):
# ....
self.vertices = vertices
self.pointlist = [self.vertices]
vertices
在这里始终是一个元组,它又包含您的 Point()
对象。元组从不具有 x
和 y
属性;它本身不是 Point
实例。
你也可以把这里的self.pointlist
换成self.vertices
。如果您需要可变序列,请使用 list()
而不是列表文字:
self.pointlist = list(self.vertices)
它使用元组中的元素创建一个列表。