Problem with Lexical Scope

bonono at gmail.com bonono at gmail.com
Mon Dec 12 04:31:09 EST 2005


jslowery at gmail.com wrote:
> Thanks for the example code. Definately provided a few different ways
> of doing the construction.
>
> Actually, the status variable was the only thing that mattered for the
> inner function.  The first code post had a bug b/c I was first just
> trying to use reduce. However, I realized that the boolean reducer
> ended up using the boolean result instead of the next field for
> subsequent operations.
>
> The effect I was going for was was a logical AND of all the returned
> values from reducer. However the inner function is supposed to use the
> same type algorithm as the built-in reduce function.
>
> reducer does have no side effects so I suppose short-circuting it would
> be the best thing. I think the only thing about the last example is
> that it starts things off with a zero. I think that would boink it.
>
> The way that this thing works is it's a general algorithm for boolean
> operations that are applied to fields in a dict.
>
> def lt(*fields):
>     return collect(fields, lambda x, y: x < y)
>
> data = {
>   'birth_date' : 19740201,
>   'hire_date' : 19840721,
>   'fire_date' :   19850123
> }
>
> rule = lt('birth_date', 'hire_date')
> assert rule(data) == True

I usually solve this kind of thing by giving reduce a tuple(or n-uples)
as the accumulator then factor out only the needed final result sans
these "temp variables".




More information about the Python-list mailing list