replace mothod for only one object but not for a class

Miki miki.tebeka at gmail.com
Tue Oct 14 14:17:24 EDT 2008


from functools import partial

class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y

    def show(self, n):
        for i in range(n):
            print "Point: (%s, %s)" % (self.x, self.y)

def new_method(obj, func):
    def method(*args, **kw):
        return func(obj, *args, **kw)

    return method


p1 = Point(1, 2)
p2 = Point(3, 4)

def show(self, n):
    print "Not a Point: %s-%s" % (self.x, self.y)
p2.show = partial(show, p2)

p1.show(3)
p2.show(3)

HTH,
--
Miki <miki.tebeka at gmail.com>
http://pythonwise.blogspot.com



More information about the Python-list mailing list