Extending Python Syntax with @

Kyler Laird Kyler at news.Lairds.org
Thu Mar 11 13:09:34 EST 2004


Richie Hindle <richie at entrian.com> writes:

>[Cameron]
>> Whenever you feel like a lambda, define a named function; 

>[Kyler]
>> How do you cleanly do that?
>> 	foo = range(-10, 10)
>> 	my_op = lambda x: float(x) / max(map(abs, foo))
>> 	bar = map(my_op, foo)

>foo = range(-10, 10)
>def my_op(x):
>    return float(x) / max(map(abs, foo))
>bar = map(my_op, foo)

>...did I misunderstand?

Well, your solution depends on a global variable.  That's going to
get *really* ugly if we move beyond the trivial example given here.
There are other problems but they're apparently not obvious with 
that example.

How 'bout some non-working code to shine some more light on it?  (If
pressed, I should be able to come up with similar code that works!)

	def make_translation_function(GCPs, type, invert=False):
		if type == 'LSF' and len(GCPs) < 12:
			# Do lots of time-consuming magic to calculate A, B, ...
			return(
				lambda x, y: (
					x * A + y * B +
					x * y * C +
					...,
					x * G + y * H +
					x * y * I +
					...
				)
			)
		elif ... # Repeat lots of times for variations.

	map_im = foo.open('map.tif')
	map_translation = make_translation_function(map_im.get_GCPs, type='LSF')

	path_im = foo.open('path.tif')
	path_translation_i = make_translation_function(path_im.get_GCPs, type='LSF', invert=True)

	# Mark all points on map that are red in path.
	for (x, y) in all_red_pixels(path_im):
		(lon, lat) = path_translation_i(x, y)
		(x, y) = map_translation(lon, lat)
		map_im.putpixel((x, y), mark_val)

Now, let's pretend that this is part of a larger program that's doing lots
of things with, potentially, lots of images, and let's also say that we
allow user-defined/imported extensions to make_translation_function().
How are you going to make a *clean* solution using named functions?

--kyler



More information about the Python-list mailing list