arn*_*bpl 1 python methods class
这是一个基本问题.我写了以下代码:
class Point:
def __init__(self,x=0,y=0):
self.x=x
self.y=y
def __str__(self):
return '({0} , {1})'.format(self.x,self.y)
def reflect_x(self):
return Point(self.x,-self.y)
p1=Point(3,4)
p2=p1.reflect_x
print(str(p1),str(p2))
print(type(p1),type(p2))
Run Code Online (Sandbox Code Playgroud)
这里p1的类型和p2的类型是不同的.我只想将p2作为一个点,它是x轴的p1反射点.我该怎么做?
我只想将p2作为一个点,它是x轴的p1反射点.我该怎么做?
好吧,那么你应该调用方法reflect_x上p1,并在存储结果p2,就像这样:
p2 = p1.reflect_x()
Run Code Online (Sandbox Code Playgroud)
在您的示例代码中,您执行了不同的操作:
p2 = p1.reflect_x
Run Code Online (Sandbox Code Playgroud)
这意味着你想要p2包含p1的reflect_x方法.