code blocks in Python

root root at triplepc.com
Tue Nov 25 05:23:29 EST 2003


Python could perfectly allow this syntax directly into the
> language, something like:
>
> codeblock my_codeblock:
>     x = x + 1
>     y = y + 2
>
> so that users won't need to explicitly call the compile() function.

It occurs to me that you're not thinking through *why* a user uses the compile function.  They use it because they have a runtime dynamic string that needs compiling.  Using a codeblock as you've defined it would mean that the code pre-existed runtime (was infact programmed before runtime).  This may not be true.

For example, show me how your syntax would fix this:

def do_functions_on_data(data):
	def mycompile(f):
		return compile(data, 'err.txt', 'exec')
	db = databaseconnection()
	functions = db.fetch_functions("WHERE Type = %d" % data.get_type())
	functions = [ mycompile(function) for function in functions ]
	return [ eval(function, locals()) for function in functions ]

It makes certain assumptions, but it works (assuming no typos; its from memory) ... fwiw.  For the confused, it fetches a list of functions that apply to the current type of data and process it with each, returning the list of results.  How would your proposed code-blocks make this simpler or even be useful?  That's all I'm wondering.

-- 
Michael T. Babcock







More information about the Python-list mailing list