replace a method in class: how?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Blais

    #1

    replace a method in class: how?

    Hello,

    I want to replace a method in a class during run-time with another function. I tried
    the obvious, but it didn't work:

    class This(object):
    def update(self,val ):
    print val

    def another_update( obj,val):
    print "another",v al

    t=This()
    t.update(5)
    t.update=anothe r_update
    t.update(5) # this one doesn't work, gives
    # TypeError: another_update( ) takes exactly 2 arguments (1 given)


    clearly it isn't seeing it as a method, just an attribute which happens to be a
    function. Is there a preferred way to do this?


    thanks,

    Brian Blais


    --
    -----------------

    bblais@bryant.e du

  • John Machin

    #2
    Re: replace a method in class: how?

    On 27/06/2006 9:14 AM, Brian Blais wrote:[color=blue]
    > Hello,
    >
    > I want to replace a method in a class during run-time with another
    > function. I tried the obvious, but it didn't work:
    >
    > class This(object):
    > def update(self,val ):
    > print val
    >
    > def another_update( obj,val):
    > print "another",v al
    >
    > t=This()
    > t.update(5)
    > t.update=anothe r_update
    > t.update(5) # this one doesn't work, gives
    > # TypeError: another_update( ) takes exactly 2 arguments (1 given)
    >
    >
    > clearly it isn't seeing it as a method, just an attribute which happens
    > to be a function. Is there a preferred way to do this?
    >[/color]

    You have a strange definition of "obvious". You say you want to replace
    a method in a *class*, not in an instance of that class ... so just do that:

    |>> class This(object):
    .... def update(self,val ):
    .... print val
    ....
    |>> def another_update( obj,val):
    .... print "another",v al
    ....
    |>> This.update = another_update
    |>> t = This()
    |>> t.update(42)
    another 42

    Cheers,
    John

    Comment

    • Bruno Desthuilliers

      #3
      Re: replace a method in class: how?

      Brian Blais a écrit :[color=blue]
      > Hello,
      >
      > I want to replace a method in a class during run-time with another
      > function. I tried the obvious, but it didn't work:
      >
      > class This(object):
      > def update(self,val ):
      > print val
      >
      > def another_update( obj,val):
      > print "another",v al
      >
      > t=This()
      > t.update(5)
      > t.update=anothe r_update[/color]

      This is not "replacing a method in a class", but replacing it in an
      instance.
      [color=blue]
      > t.update(5) # this one doesn't work, gives
      > # TypeError: another_update( ) takes exactly 2 arguments (1 given)[/color]
      [color=blue]
      >
      > clearly it isn't seeing it as a method, just an attribute which happens
      > to be a function. Is there a preferred way to do this?[/color]

      import types
      t.update = types.MethodTyp e(another_updat e)

      Comment

      • nate

        #4
        Re: replace a method in class: how?

        I don't know a ton about this, but it seems to work if you use:
        This.update = another_update
        (instead of t.update = another_update)
        after saying This.update = another_update, if you enter This.update, it
        says <unbound method This.another_up date>, (it's unbound)
        but if you call
        t.update(5)
        it will print:
        another 5

        Comment

        • Maric Michaud

          #5
          Re: replace a method in class: how?

          Le mardi 27 juin 2006 05:05, Bruno Desthuilliers a écrit :[color=blue]
          > import types
          > t.update = types.MethodTyp e(another_updat e)[/color]

          This works with :

          t.update = types.MethodTyp e(another_updat e, t)

          Oh, this *really* misleading what the intent of the programmer is, and

          In [29]: t.update
          Out[29]: <bound method ?.<lambda> of <__main__.a instance at 0xa774d96c>>

          all let think it's a method defined in t's class.
          I prefer early binding with default parameters :

          In [30]: def another_update( self, param) :pass
          ....:

          In [31]: t.update = lambda x, self=t : another_update( self, x)

          In [32]: t.update = lambda x, self=t, func=another_up date : func(self, x)

          In [33]: t.update
          Out[33]: <function <lambda> at 0xa7744aac>

          So we have a function and know it (probably) belongs to the instance.

          --
          _____________

          Maric Michaud
          _____________

          Aristote - www.aristote.info
          3 place des tapis
          69004 Lyon
          Tel: +33 426 880 097

          Comment

          • Bruno Desthuilliers

            #6
            Re: replace a method in class: how?

            Maric Michaud a écrit :[color=blue]
            > Le mardi 27 juin 2006 05:05, Bruno Desthuilliers a écrit :
            >[color=green]
            >>import types
            >>t.update = types.MethodTyp e(another_updat e)[/color]
            >
            >
            > This works with :
            >
            > t.update = types.MethodTyp e(another_updat e, t)[/color]

            oops ! too fast on the send button :(
            And thanks for the correction.

            Comment

            Working...