Adding new methods at runtime to a class

val val at vtek.com
Mon Nov 24 17:25:10 EST 2003


"Roy Smith" <roy at panix.com> wrote in message
news:roy-A5FC3C.16223824112003 at reader2.panix.com...
> 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.

Tim,
    Yes and No.  In the *real-world* programming,
including embedded systems and environment-driven systems,
often you have no idea what may happen next, and you don't have
a luxury to design and build your program ahead.   You still
can analyze the current (run-time) situation and build your response
on-the-fly, dynamically.  That's where the power of dynamic
languages comes in, and my simple understanding is that
the reality gets more and more dynamic.
    To me, it does not sound crazy that the compiled
languages/programs will be forced to die out gradually.
Python is kind of half-compiled (extensions) and half-interpreted
with the interpretation 'half' in control.  However, the coming
reality with lots of indirect, revealed-at-run-time relationships
and inputs will require even more dynamic (than Python) languages.
And 'dynamic' means the code being built on-the-fly with lots
of inputs from environment.
    Some recent developments such HP's Dynamo, Transmeta's Crusoe
(with Linus Torvalds?), Sun's MAJC indicate these kind of trends.
And the reason for this trend is performance and dynamics, the
performance being the number of *right* (million) instructions
per second, not *any* MIPS, coz you want the *right* result,
not just a result fast.
    In fact, the heart of any computer is an interpereter -
an instruction decoder that decodes/executes (always at run-time)
a program counter controlled instruction which is either available
in memory, or gets built dynamically.  Remember the very simple
von Neumann's dynamic re-addressing; it still rules the computer
world.  So, i guess the point is the *level* of the instruction
and the power of the decoder (computer interpreter).  Python might
be a *directly interpreted* language like many other languages
since the 60s.
    To me, Roy sounds convincing.
Correct me coz i'm used to be wrong...
val






More information about the Python-list mailing list