Adding method at runtime - problem with self
Schüle Daniel
uval at rz.uni-karlsruhe.de
Sun Mar 5 20:13:53 EST 2006
marek.rocki at wp.pl wrote:
> 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
>
k.foo(k)
would work
More information about the Python-list
mailing list