【发布时间】:2018-10-14 06:12:17
【问题描述】:
我正在尝试学习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 后更改它?
【问题讨论】:
-
是什么让你说你不能?
-
顺便说一下可能名字乱码