我不太了解python(之前从未使用过它:D),但我似乎无法在网上找到任何东西.也许我只是没有谷歌正确的问题,但在这里我去:
我想更改实例的特定方法的实现.当我用Google搜索它时,我发现你可以这样做,但它改变了同一个类的所有其他实例的实现,例如:
def showyImp(self):
print self.y
class Foo:
def __init__(self):
self.x = "x = 25"
self.y = "y = 4"
def showx(self):
print self.x
def showy(self):
print "y = woohoo"
class Bar:
def __init__(self):
Foo.showy = showyImp
self.foo = Foo()
def show(self):
self.foo.showx()
self.foo.showy()
if __name__ == '__main__':
b = Bar()
b.show()
f = Foo()
f.showx()
f.showy()
Run Code Online (Sandbox Code Playgroud)
这不能按预期工作,因为输出如下:
x = 25
y = 4
x = 25
y = 4
我希望它是:
x = 25
y = 4
x = 25 …
python ×1