Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Adding new methods at runtime to a class

Reply
Thread Tools

Re: Adding new methods at runtime to a class

 
 
=?iso-8859-1?Q?Fran=E7ois?= Pinard
Guest
Posts: n/a
 
      11-25-2003
[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

 
Reply With Quote
 
 
 
 
Michele Simionato
Guest
Posts: n/a
 
      11-25-2003
François Pinard <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> [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
 
Reply With Quote
 
 
 
 
Michael Hudson
Guest
Posts: n/a
 
      11-25-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Creating and adding new methods in runtime konryd Ruby 2 05-11-2007 05:08 PM
Adding instance methods to class at runtime Jon A. Lambert Ruby 3 08-29-2005 09:10 PM
RE: Adding new methods at runtime to a class Delaney, Timothy C (Timothy) Python 5 11-25-2003 02:26 AM
Adding new methods at runtime to a class Fernando Rodriguez Python 1 11-24-2003 09:54 AM



Advertisments