code blocks in Python

Jeff Epler jepler at unpythonic.net
Mon Nov 24 03:24:28 CET 2003


On Sun, Nov 23, 2003 at 04:34:39PM -0800, Hung Jung Lu wrote:
> I still have yet to hear any difficulties/complaints.

Suppose I write this:

	def C:
		x = x + 1

	def a():
		x = 2
		exec C
		print x

	def b():
		exec C

how will the implementation of "code blocks" deal with the fact that in
a, x is a fastlocal with a particular index, but in b there's no local
x?  (In cpython, "bare exec" has a significant negative impact of the
performance of a function, because all variable accesses happen though
LOAD_NAME instead of LOAD_GLOBAL / LOAD_FAST.  Any new block-oriented
feature should be designed to avoid this problem)

> I refuse to believe that Python developers have NEVER considered this
> extension to the language. So, what's the problem?

Extensions along these lines have often been discussed.  Consider reading
the archives. "Macros", "code blocks", "with blocks", and "closures" are
all terms you might consider using if you are searching for past messages.

Jeff





More information about the Python-list mailing list