Adding new methods at runtime to a class

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Mon Nov 24 22:57:11 EST 2003


> From: François Pinard [mailto:pinard at iro.umontreal.ca]
> 
> [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.)

There are indeed cases where adding or replacing methods is useful - mock objects and methods in unit tests is the most common case I tend to run across in my day-to-day work.

However, it is not something people should be doing unless they understand why it is the correct thing to do. It's not quite at the level of metaclass programming, but it's definitely above casual use.

If I see someone in my team doing things like this, I want them to justify it to me (including explaining why it should be obvious to me :) Usually they can. If they can't, then something needs to be done about it.

> 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.

Indeed - but I trust that you understand *why* you're doing this, and what problems you may encounter while doing so - for example, the difference between adding a method to the class, or to a single instance of a class.

OTOH, there is no evidence that the original poster has read and understood the appropriate sections of the Python reference, cookbook, etc. Nor was the question phrased as "I'm trying to do ... and in <language> the correct idiom would be to add a method to the object - is this the appropriate way to do it in Python?". As a result I'm forced to consider the original poster as a neophyte until shown otherwise.

One reason it can be considered "black magic" is because the objects you're working with may change in unexpected ways. I expect that if I read the documentation for a class, or its definition, that it will behave as defined. If it suddenly starts doing different things (because someone modified it in some other part of the code), that's very surprising behaviour.

If it's completely localised (for example, in a unit test) then it's another matter - but the original poster hasn't qualified it in any way so I can't make that assessment.

> 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! :-)

Painful experience :)

Tim Delaney





More information about the Python-list mailing list