Adding method to a class on the fly

7stud bbxx789_05ss at yahoo.com
Fri Jun 22 17:17:45 EDT 2007


On Jun 22, 2:24 pm, askel <dummy... at mail.ru> wrote:
> class Dummy:
>   def method(self, arg):
>     print arg
>
> def method2(self, arg):
>   self.method(arg)
>
> Dummy.method2 = method2
> Dummy.method2('Hello, world!')

Traceback (most recent call last):
  File "test1.py", line 8, in ?
    Dummy.method2('Hello, world!')
TypeError: unbound method method2() must be called with Dummy instance
as first argument (got str instance
instead)



>I like that to be the same as:
>
>class Dummy:
>    def __init__(self):
>        return
>
>    def method_dynamic(self):
>        return self.method_static("it's me")
>
>    def method_static(self, text):
>        print text
>       return
>
>
>so that I can do:
>
>dum=Dummy.method_dynamic()
>
>and see "it's me" printed.

When are you able to see that?




More information about the Python-list mailing list