Adding method at runtime - problem with self

marek.rocki at marek.rocki at
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() = 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,


More information about the Python-list mailing list