Instrospection question

thebjorn BjornSteinarFjeldPettersen at gmail.com
Fri Dec 21 05:42:44 EST 2007


On Dec 21, 11:28 am, Matias Surdi <matiassu... at gmail.com> wrote:
> I have the following code:
[...]
> As you can see, what I'm trying to do is "replace" a method with another
> one wich is the same method but with a function applied to it (in this
> case, a string concatenation ( +" modified"))
>
> Can anybody help me with this?

Why not use inheritance?

class A(object):
    def a(self):
        print "Original"

class B(A):
    def a(self):
        super(B, self).a()
        print 'modified'

a = B()
a.a()

I'm not saying the code below is a good idea, but it solves the
problem of post-hoc modification

a = A()
a.__class__ = B
a.a()

we could probably come up with a better solution if we knew more about
the problem you were trying to solve...

-- bjorn



More information about the Python-list mailing list