Adding method at runtime - problem with self

marek.rocki at wp.pl marek.rocki at wp.pl
Sun Mar 5 23:13:21 CET 2006


First of all, please don't flame me immediately. I did browse archives
and didn't see any solution to my problem.

Assume I want to add a method to an object at runtime. Yes, to an
object, not a class - because changing a class would have global
effects and I want to alter a particular object only. The following
approach fails:

class kla:
    x = 1

def foo(self):
    print self.x

k = kla()
k.foo = foo
k.foo()

I know where the problem is. The method shouldn't have 'self'
parameter. But how do I access object's attributes without it?

Best regards,

Marek




More information about the Python-list mailing list