code blocks in Python

Hung Jung Lu hungjunglu at yahoo.com
Mon Nov 24 01:20:05 EST 2003


Jeff Epler <jepler at unpythonic.net> wrote in message news:<mailman.1009.1069640669.702.python-list at python.org>...
> 	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 agree that performance could be made better. But that's an issue for
later. If codeblock becomes a fundamental building block of a language
(say, Python 3.0), I am sure there are tricks beyond the current
global/local/builtin namespaces mechanisms
(LOAD_NAME/LOAD_FAST/LOAD_GLOBAL). But performance is an issue for
later. I would like to remind people that the compile() function
exists and works in Python. For whatever discussion on this subject,
the first thing to do is to replace the codeblock definition with the
compile function() and analyze from there on. In this case,
C=compile('x=x+1\n', '<string>', 'exec').

regards,

Hung Jung




More information about the Python-list mailing list