我正在学习Python中的绑定方法,并已经实现了下面的代码:
class Point:
def __init__(self, x,y):
self.__x=x
self.__y=y
def draw(self):
print(self.__x, self.__y)
def draw2(self):
print("x",self.__x, "y", self.__y)
p1=Point(1,2)
p2=Point(3,4)
p1.draw()
p2.draw()
p1.draw=draw2
p1.draw(p1)
当我运行这段代码时,将产生以下输出:
1 2
3 4
Traceback (most recent call last):
File "main.py", line 17, in <module>
p1.draw(p1)
File "main.py", line 10, in draw2
print("x",self.__x, "y", self.__y)
AttributeError: 'Point' object has no attribute '__x'
为什么我不能改变p1.draw(),让它指向draw2之后再改变呢?