Adding new methods at runtime to a class
roy at panix.com
Mon Nov 24 22:22:38 CET 2003
In article <mailman.1036.1069707396.702.python-list at python.org>,
"Delaney, Timothy C (Timothy)" <tdelaney at avaya.com> wrote:
> > 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.
> Tim Delaney
Why is it unpythonic to add methods at runtime? That's the nature of a
dynamic language. If foo.bar doesn't have to exist until the moment
it's evaluated, why should foo.bar() have to? Or maybe a better way to
ask it is why should foo.bar have to be callable at any other time than
when you evaluate foo.bar()?
I would say that the fact that all of a class's methods have to be
declared at once is itself somewhat unpythonic.
More information about the Python-list