Skip to main content
 首页 » 编程设计

python之为什么我从类似 `instance.method` 的代码中得到了错误的结果

2025年05月04日13JustinYoung

<分区>

这是一个基本问题。 我写了下面的代码:

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)) 

这里p1的类型和p2的类型是不同的。我只想要 p2 作为 p1 从 x 轴的反射点。我该怎么做?