Re: Adding new methods at runtime to a class

Discussion in 'Python' started by =?iso-8859-1?Q?Fran=E7ois?= Pinard, Nov 25, 2003.

  1. [Delaney, Timothy C (Timothy)]
    > > From: Fernando Rodriguez


    > > How can I add new methods at runtime to a class?


    > Before we send you into what most would consider black magic in
    > Python, you should explain why you want to. In most cases, this is
    > *not* what you want to do - there are better, more elegant and much
    > more Pythonic ways of doing it.


    There is a case which happens sometimes for me, in which I find it
    useful assigning methods at runtime. It occurs in stateful objects,
    where the actions of conceptual method varies according to the state.
    It is clean, clear and fast to merely assign various processors to
    methods when the state changes, more than maintaining and testing a
    state variable all-around in a never changing set of methods. (What
    does not change is the external API.)

    To me at least, it does not look like black magic, I find it Pythonic
    enough, and even elegant, to the point I'm doing more and more. Oh, I
    do read your assertion about what people do "in most cases", but I'm not
    sure how you get such statistics. Please enlighten us! :)

    --
    François Pinard http://www.iro.umontreal.ca/~pinard
     
    =?iso-8859-1?Q?Fran=E7ois?= Pinard, Nov 25, 2003
    #1
    1. Advertising

  2. François Pinard <> wrote in message news:<>...
    > [Delaney, Timothy C (Timothy)]
    > > > From: Fernando Rodriguez

    >
    > > > How can I add new methods at runtime to a class?

    >
    > > Before we send you into what most would consider black magic in
    > > Python, you should explain why you want to. In most cases, this is
    > > *not* what you want to do - there are better, more elegant and much
    > > more Pythonic ways of doing it.

    >
    > There is a case which happens sometimes for me, in which I find it
    > useful assigning methods at runtime. It occurs in stateful objects,
    > where the actions of conceptual method varies according to the state.
    > It is clean, clear and fast to merely assign various processors to
    > methods when the state changes, more than maintaining and testing a
    > state variable all-around in a never changing set of methods. (What
    > does not change is the external API.)
    >
    > To me at least, it does not look like black magic, I find it Pythonic
    > enough, and even elegant, to the point I'm doing more and more. Oh, I
    > do read your assertion about what people do "in most cases", but I'm not
    > sure how you get such statistics. Please enlighten us! :)


    I was thinking about an even more frightening possibility: changing the
    class of the stateful object! I.e. there would be a different class
    for each state (hopefully collected in a common hierarchy) and one
    would change the class of "self" according to some condition. This
    would automatically accomplish the change of the methods according
    to the state, and would give the additional benefit of inheritance
    (i.e. methods could call they supermethods quite easily).

    I haven't written anything yet, since I am not sure if it would be
    a good idea in practice, however I wonder if somebody ever tried it.

    The message is "We got a dynamic language: let use it!"

    Michele
     
    Michele Simionato, Nov 25, 2003
    #2
    1. Advertising

  3. (Michele Simionato) writes:

    > I was thinking about an even more frightening possibility: changing the
    > class of the stateful object! I.e. there would be a different class
    > for each state (hopefully collected in a common hierarchy) and one
    > would change the class of "self" according to some condition. This
    > would automatically accomplish the change of the methods according
    > to the state, and would give the additional benefit of inheritance
    > (i.e. methods could call they supermethods quite easily).
    >
    > I haven't written anything yet, since I am not sure if it would be
    > a good idea in practice, however I wonder if somebody ever tried it.
    >
    > The message is "We got a dynamic language: let use it!"


    I *think* I'd rather change an attribute of the instance for each
    state change and forward onto that:

    def move_state(self):
    self.state = State()

    def do_something_dependent_on_state(self):
    self.state.do_something()

    Cheers,
    mwh

    --
    Premature optimization is the root of all evil.
    -- Donald E. Knuth, Structured Programming with goto Statements
     
    Michael Hudson, Nov 25, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fernando Rodriguez

    Adding new methods at runtime to a class

    Fernando Rodriguez, Nov 24, 2003, in forum: Python
    Replies:
    1
    Views:
    276
    Peter Otten
    Nov 24, 2003
  2. Delaney, Timothy C (Timothy)

    RE: Adding new methods at runtime to a class

    Delaney, Timothy C (Timothy), Nov 24, 2003, in forum: Python
    Replies:
    5
    Views:
    309
  3. Jon A. Lambert
    Replies:
    3
    Views:
    128
    Jon A. Lambert
    Aug 29, 2005
  4. konryd
    Replies:
    2
    Views:
    140
    Ken Bloom
    May 11, 2007
  5. Kenneth McDonald
    Replies:
    5
    Views:
    387
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page