Perl/Python/Ruby common backend (Parrot, can Ruby play too?)

Erno Kuusela erno-news at erno.iki.fi
Tue Aug 7 19:36:28 EDT 2001


In article <9kpeft$kbm$1 at slb4.atl.mindspring.net>, "Andrew Dalke"
<dalke at acm.org> writes:

[...]
| She chose Tcl (with an
| object extension) over Python because Python's ability to add
| new methods is not obvious[*] and because existing instances
| are not updated with the new class definitions

you can do this:

>>> class Spam:
...     def eat(self):
...         print 'yum'
... 
>>> spam = Spam()
>>> spam.eat()
yum
>>> 
>>> def new_eat(self):
...     print 'yech'
... 
>>> Spam.eat = new_eat
>>> 
>>> spam.eat()
yech

if you want it to attach the new method to the same class in face of
inheritance, you need "spam.eat.im_class.eat = new_eat" - you could
wrap it in a function if that look too much like a magic incanatation
for your audience.

if you actually want to replace the underlying classes of all
instances, you could probably cook something up with the weakref
module and calling a registration function in __init__...

  -- erno







More information about the Python-list mailing list