explain this function to me, lambda confusion
Lie
Lie.1296 at gmail.com
Sun May 18 09:23:49 EDT 2008
On May 9, 8:57 am, andrej.panj... at climatechange.qld.gov.au wrote:
> On May 8, 6:11 pm, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
>
>
>
> > No, no, no, no, no!
>
> Geez. Go easy.
>
>
>
> > You have got it entirely wrong here. Your XOR function simply returns a
> > function which gives you the result of xoring the parameters AT THE TIME
> > WHEN YOU ORIGINALLY CREATED IT. I'm guessing that you had already set
> > cream and icecream (otherwise the call to XOR would have thrown an
> > exception) and at leas one was true. Try setting them both False at the
> > beginning:
>
> > >>> cream = False
> > >>> icecream = False
> > >>> topping = XOR( cream, icecream)
> > >>> cream = True
> > >>> icecream = False
> > >>> print topping()
>
> > False
>
> Ok. I understand this better now. I did say I found the documentation
> rather terse on this.
>
> > Using a lambda was a completely pointless exercise here, you could have
> > just returned the result directly:
>
> If I try out a new language, I try to exercise those parts of the
> language that are new to me. Now I saw lambdas, an interesting
> structure I hadn't seen before. So I tried them out. I get to learn a
> little at the same time as scripting. That was the "point". I only
> get to optimise my use of a language by trying out various corners of
> it.
>
>
>
> > def TFF(x,y,z) :
> > return x and not y and not z
>
> > AddOnly = TFF( options.AddAction, options.ReplaceAction,
> > options.DeleteAction )
> > DeleteOnly = TFF( options.DeleteAction, options.AddAction,
> > options.ReplaceAction )
> > ReplaceOnly = TFF( options.ReplaceAction, options.AddAction,
> > options.DeleteAction )
>
> > if not (DeleteOnly or AddOnly or ReplaceOnly):
> > print "Error: Exactly one of [ --add | --replace | --delete ]
> > allowed. "
> > parser.print_help()
> > exit
>
> > which boils down to:
>
> > if (options.AddAction + options.ReplaceAction +
> > options.DeleteAction) != 1:
> > print "Error: ..."
>
> Indeed, there are many ways this could be done. Some are more
> concise, some are more efficient. As I said, I did it the way I did
> it to try out lambdas. Your way achieves the result, rather elegantly
> I think, but teaches me nothing about using lambdas.
>
> Pardon my tetchiness, but it is a little hard to receive such blunt
> and inflexible replies to my posts.
>
> Both the responses offer lambda free alternatives. That's fine, and
> given the terse documentation and problems that I had understanding
> them, I would agree. So what applications are lambdas suited to? I
> think the parameterised function model is one.
> What else?
Lambda can actually be safely removed from python and no other
features would be missing. It is always possible to create a def
version of any lambda, so lambda is useless. It is just a convenience
for the times where we're just too lazy to invent a name and find a
place to place the def, instead just inlining the function.
More information about the Python-list
mailing list