So now some Pythonistas want monkeypatching! Will the snakes recoil? Hardly, in the comments, Harry Fuecks offers up:
class Foo:
def __init__(self):
self.x = "x = 1"
self.y = "y = 2"
def showx(self):
print self.x
def showy(self):
print self.y
if __name__ == '__main__':
# Attach at runtime...
Foo.showy = showy
f2 = Foo()
f2.showx()
f2.showy()
It’s priceless how Harry’s code gets flattened in the comments. I mean this is one example where the nesting is really essential. I’m not saying my comment layout is any better. We’ve had a real mess with plus signs here. (Nicked from Simon.)