Python 2.7将对象传递给类函数
我试图用python测试我的2D坐标和向量类。以下是定义向量和坐标类的代码:Python 2.7将对象传递给类函数,python,python-2.7,object,parameter-passing,Python,Python 2.7,Object,Parameter Passing,我试图用python测试我的2D坐标和向量类。以下是定义向量和坐标类的代码: class coord(object): def __init__(self,x,y): self.x = x self.y = y def resolve(endCoord): return vector((self.x-endCoord.x),(self.y-endCoord.y)) class vector(object): def __i
class coord(object):
def __init__(self,x,y):
self.x = x
self.y = y
def resolve(endCoord):
return vector((self.x-endCoord.x),(self.y-endCoord.y))
class vector(object):
def __init__(self, xTrans, yTrans):
self.xTrans = xTrans
self.yTrans = yTrans
self.magnitude = sqrt((self.xTrans**2)+(self.yTrans**2))
然后,我用下面的语句测试它们:
inp1 = raw_input("Please enter the first coordinate: ")
inp2 = raw_input("Please enter the second coordinate: ")
coord1 = coord(int(inp1[0]), int(inp1[2]))
coord2 = coord(int(inp2[0]), int(inp2[2]))
vector1 = coord1.resolve(coord2)
print "Vector magnitude is "+str(vector1.magnitude)
我对线路有问题:
vector1 = coord1.resolve(coord2)
在抛出此错误的位置:
exceptions.TypeError: resolve() takes exactly 1 argument (2 given)
我不知道怎么修理它。我给出的inp1为“0,0”(无引号),inp2为“5,5”(同样无引号)
我想这可能是一个问题,要么是作为函数参数给出一个对象,要么是作为函数参数给出一个坐标,当函数在坐标类中时
我真的不知道,任何帮助都将不胜感激 解析的第一个参数应该是
self
class coord(object):
...
def resolve(self, endCoord):
return vector((self.x-endCoord.x),(self.y-endCoord.y))
所有方法(与函数类似,但在类中)都将第一个参数接受为self
,如\uuuu init\uuu()
方法所示
def resolve(endCoord):
应该是
def resolve(self, endCoord):
看