Become

Kragen Sitaker kragen at canonical.org
Mon Nov 26 16:11:33 EST 2001


Skip Montanaro <skip at pobox.com> writes:
>     Jiba> Here is a module that reproduce the Smalltalk "become" capability.
>     Jiba> Become can replace (quite) all references to an object by a
>     Jiba> reference to another object. It relies on gc.get_referents().
> 
> Can you motivate this with an example that shows where it would be useful?

E-style promises are one example; see http://www.erights.org/ for
details.  Autoloading of code or other objects from on-disk files are
another.  Lazy evaluation is a third: once an object has been fully
evaluated, it would be nice to replace the lazy object with its
non-lazy version.  Just-in-time optimization of code is a fourth: once
the optimized version of a callable routine is ready, you'd like to
replace the unoptimized version with it, so that all the callers will
run the optimized version.  A fifth application is a saner set of
semantics for reload(), which currently doesn't play well with 'from x
import y'.  With 'become', reload() could work better.

In Python 1.5.2, 'become' for class instances is fairly
straightforward: you set __class__ and __dict__ to the right values
and you're done.  But 'become' for built-in types is tough; you can't
even write a Proxy object that supports all the protocol a number
supports (there's no way to override three-argument pow(), for crying
out loud!  And various C functions complain when given an argument
that acts like an int or a float instead of actually being one).

I haven't looked at trying to write 'become' in Python 2.x.  I suspect
it will be more difficult already, even for class instances, and it
will get more difficult over time.

Jiba's approach is very interesting, although it's inherently somewhat
inefficient, and I don't think it can work well without cooperation
from CObject authors.  (You need to be able to find out which slots in
the CObject refer to PyObjects and which contain character data or
integers; otherwise, you're liable to change things other than
PyObject pointers.)




More information about the Python-list mailing list