Python 如何通过调用同一类Vector中定义的方法初始化Vector实例?
所以我想初始化一个向量类的实例,通过该类中定义的方法返回一个元组Python 如何通过调用同一类Vector中定义的方法初始化Vector实例?,python,Python,所以我想初始化一个向量类的实例,通过该类中定义的方法返回一个元组 class Point(object): def __init__(self, x, y): self.x = x self.y = y class Vector(object): def __init__(self, x, y): self.x = x self.y = y def subtract(self, a, b):
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
class Vector(object):
def __init__(self, x, y):
self.x = x
self.y = y
def subtract(self, a, b):
x = a.x - b.x
y = a.y - b.y
return x, y # <-- This tuple
p = Point(0, -1)
i = Point(1, 1)
# Here I want to call Vector.subtract(p, i) and assign this tuple to a Vector instance
类点(对象):
定义初始化(self,x,y):
self.x=x
self.y=y
类向量(对象):
定义初始化(self,x,y):
self.x=x
self.y=y
def减去(自身、a、b):
x=a.x-b.x
y=a.y-b.y
返回x,y 35;为什么不重写您的方法
def subtract(self, a, b):
x = a.x - b.x
y = a.y - b.y
return x, y # <-- This tuple
所以你可以打电话
a = Vector(1,2)
b = Vector(4,1)
c = a.substract(b)
或者至少通过删除self
引用使其成为静态方法
@staticmethod
def subtract(a, b):
x = a.x - b.x
y = a.y - b.y
return Vector(x, y) # <-- result as new Vector
你为什么不重写你的方法
def subtract(self, a, b):
x = a.x - b.x
y = a.y - b.y
return x, y # <-- This tuple
所以你可以打电话
a = Vector(1,2)
b = Vector(4,1)
c = a.substract(b)
或者至少通过删除self
引用使其成为静态方法
@staticmethod
def subtract(a, b):
x = a.x - b.x
y = a.y - b.y
return Vector(x, y) # <-- result as new Vector
你为什么不重写你的方法
def subtract(self, a, b):
x = a.x - b.x
y = a.y - b.y
return x, y # <-- This tuple
所以你可以打电话
a = Vector(1,2)
b = Vector(4,1)
c = a.substract(b)
或者至少通过删除self
引用使其成为静态方法
@staticmethod
def subtract(a, b):
x = a.x - b.x
y = a.y - b.y
return Vector(x, y) # <-- result as new Vector
你为什么不重写你的方法
def subtract(self, a, b):
x = a.x - b.x
y = a.y - b.y
return x, y # <-- This tuple
所以你可以打电话
a = Vector(1,2)
b = Vector(4,1)
c = a.substract(b)
或者至少通过删除self
引用使其成为静态方法
@staticmethod
def subtract(a, b):
x = a.x - b.x
y = a.y - b.y
return Vector(x, y) # <-- result as new Vector