Why I hate lambdas (Re: Do any of you recommend Python as a first programming language?)
Roy Smith
roy at panix.com
Sun Mar 23 22:36:35 EDT 2008
Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> wrote:
> On Sun, 23 Mar 2008 13:51:34 -0400, Roy Smith wrote:
>
> > On the other hand, when I do:
> >
> > def torture():
> > woman.putInChair()
> > cushion.poke()
> > rack.turn()
> >
> > I've also done two things. First, I've created a function object (i.e.
> > a lambda body), and I've also bound the name torture to that function
> > object, in much the same way I did with the list. But, it's different.
> > The function object KNOWS that it's name is torture.
>
> No it does not. Function objects don't know their name. All they know is
> that they have a label attached to them that is useful to use as a name
> in some contexts, e.g. when printing tracebacks. It's just a label,
> nothing more.
I think we're arguing the same thing. When you write
def foo():
whatever
you create an object which contains the string "foo", retrievable through
its __name__ attribute. That's what I meant by "it knows its name"
>> What Python give us with lambdas is some half-way thing. It's not a
>> full function, so it's something that people use rarely,
>
> Which people?
>
> > which means most people (like me) can't remember the exact syntax.
>
> Speak for yourself, not for "most people".
Well, OK. When I said, "most people", I really meant "I know about me, and
I'm guessing about other people". I still think it's a fair statement that
if you look any large collection of Python code, you will find many more
uses of def than of lambda.
> > Even when I know
> > it's the right thing to be using in a situation, I tend not to use it
> > simply because the path of least resistance is to write a one-off
> > function vs. looking up the exact syntax for a lambda in the manual.
>
> lambda arguments : expression
>
> Why is that harder to remember than this?
>
> def name ( arguments ) :
> block
because I remember things I use often, better than I remember things I use
infrequently.
More information about the Python-list
mailing list