Extending Python Syntax with @

Myles myles at geocities.com
Thu Mar 11 19:05:40 EST 2004


Kyler Laird <Kyler at news.Lairds.org> writes:

> Richie Hindle <richie at entrian.com> writes:
>  
>  [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)
> 
> Well, your solution depends on a global variable.

foo ? my_op ?
Doesn't the same apply to the lambda example ?

> 	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.

Isn't this just a more detailed example of the same thing ?
With a similar solution :

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, ...
        def translation_fn(x, y):
            return (
               x * A + y * B +
               x * y * C +
               ...,
               x * G + y * H +
               x * y * I +
               ...
           )
        return translation_fn
    elif ... # Repeat lots of times for variations.

Quick variation using working code:

def makefn(a, b):
    A = a*3
    B = b * 2
    def my_op(x, y):
        return (x*A + y*B, (x+A)*(y+B))
    return my_op

x = makefn(1, 1)
print x(2, 2)

Regards, Myles.



More information about the Python-list mailing list