[sapug] Erlang, the movie.

Daryl Tester Daryl.Tester at iocane.com.au
Wed Oct 4 01:27:28 CEST 2006


Chris Foote wrote:

> The voice peering project I'm working on has an embedded interpreter
> running in a seperate thread in 'screen' sessions (using code.interact() )

Reminding me of code.interact() brings up painful memories.  :-)  It's the
second time where I've coded up something only to find out there was an
equivalent method in the standard library (at the time of the quilter
talk, my REPL was implemented using exec and eval, but I wasn't happy
with it. Then I found HTTPREPL which pointed me in the appropriate
direction).  It annoys me, because when learning a language I make sure
I go over the library in detail so I don't reinvent any unneccessary
wheels.

> It's a shame that it's not usable outside of reloading a module:
> 
>>>> class C(object):
> ...     def method(self):
> ...             return '1st method version'
> ...
>>>> c = C()
>>>> c.method()
> '1st method version'
>>>>
>>>> def newmethod(self):
> ...     return '2nd method version'
> ...
>>>> C.method = newmethod
>>>>
>>>> c.method()
> '2nd method version'
>>>>
> 
> i.e. existing object uses new version of method.

I'd expect this, because the class that the instance refers
to is being patched.  Unfortunately the copy module doesn't
appear to touch classes, but a possible workaround is to
subclass C with the new method, then assign that to C.

e.g. -

>>> class C(object):
...     def method(self):
...         return '1st method version'
...
>>> c = C()
>>> c.method()
'1st method version'
>>> class C1(C):
...   def method(self):
...       return '2nd method version'
...
>>> C = C1
>>> c.method()
'1st method version'
>>> c1 = C()
>>> c1.method()
'2nd method version'

If a module is used there is no need to subclass the original
class, as the new class is defined in what is effectively a
new namespace (same name, but any existing instances hold
references to the old namespace).

> The Common LISP object system has a nifty way to upgrade existing objects
> by invoking a 'class-changed' method.[1]  CL makes my brain hurt :-(

That it does (as does Richard Gabriel).


-- 
Regards,
  Daryl Tester, IOCANE Pty. Ltd.


More information about the sapug mailing list