Could Emacs be rewritten in Python?

Jeff Epler jepler at unpythonic.net
Wed Apr 9 23:00:07 EDT 2003


On Wed, Apr 09, 2003 at 08:44:29PM +0100, Alexander Schmolck wrote:
> Beni Cherniavsky <cben at techunix.technion.ac.il> writes:
> 
> > It also describes some implementation details - not that I believe we
> > want dynamic scope...  
> 
> Why not? Amongst other things dynamically scoped global variables seems the
> only sane way to me to have global variables. Otherwise, everytime you screw
> around with some global variable temporarily you have to go through all sorts
> of pain making sure you clean it up after you're done (even if something went
> wrong) and you'd better not be multithreaded. Compare:
> 
> # simple, single threaded case
> try:
>    global precision
>    old = precision
>    precision = 10e-10
>    dosomething()
> finally:
>    precision = old
> 
> (let ((precision 10e-10)) (dosomething))

Ignoring the fact that "globals" are module-level, you can obtain
"stacking" of global variables with only a single level of name lookup,
including automatic removal on return, in Python.  All that you need is a
convenent way to write complex things in the 'body' argument.  Untested
code, of course

_let_undef = object()
def let(body, **kw):
	restore = {}
	g = globals()
	for k, v in kw.items():
		restore[k] = g.get(k, _let_undef)
		g[k] = v
	try:
		return body()
	finally:
		for k, v in restore.items():
			if v is _let_undef:
				del g[k]
			else:
				g[k] = v



Jeff





More information about the Python-list mailing list