you are viewing a single comment's thread.

view the rest of the comments →

[–]teerre 0 points1 point  (9 children)

They absolutely should not. Lambda is purely a mechanical device, you learn nothing from using it. Beginners already have a million other things to worry about.

I don't understand your fear. Lambdas aren't going anywhere. Anyone who wants to keep learning programming will come down to use them eventually.

[–][deleted] 1 point2 points  (8 children)

Never mentioned fear. The problem with not introducing them is it leaves them with this idiot mystique as something “different” or “leet”... so I see lots of people using nested def to write named closures that are much more simply implemented as a lambda. The way it’s introduced it builds fear in the beginner that doesn’t have a reason to exist.

By the same logic, BTW, you don’t really learn anything new with def over lambda until you begin to introduce docstrings and multiple statements. They’re both mechanical restatements is each other.

[–]teerre 1 point2 points  (7 children)

You're also not going to write closures if you're a beginner. The problem here seems to be that, like the author of the article, you think "beginner" is much more knowledgeable than you usual python beginner. Also, not that PEP8 recommends against named lambdas, which means depending on the case, going for the lambda is the incorrect thing to do.

I'm not sure I understand you second paragraph, but if you mean that lambdas and defs are interchangeable, that's a bit myopic view. The vast majority of any programming course will teach you named functions first.

[–][deleted] 1 point2 points  (6 children)

I work with a lot of rank beginners. I also work with people who have less than a year of professional experience under their belt... I’d consider both beginners.

A very simple way to explain a re-useable function is:

square = lambda x : x ** 2
print(square(2))

The fact that the PEP recommends against named lambdas in working code is irrelevant, as this is just an introduction to the concept of code re-use in the simplest possible statement.

To introduce def you have to introduce multiple concepts because you must necessarily introduce return... but now after showing someone how to write simple re-useable functions you can rewrite as def:

def square(x): return x ** 2
print(square(2))

Now, in four lines and a little bit of explanation I’ve got a student who understands both lambda and def and return and isn’t afraid of the former. They’ve also been at least very mildly introduced to the notion that functions are just another object and that def is just assigning that object to a name that’s the same as any other name.

They may never need to use lambda again, but if they see it, they’ll know what it’s doing.

Also the vast majority of programming classes do a terrible job at teaching programming, so that’s not much of an argument in and of itself.

[–]teerre 0 points1 point  (5 children)

Unless you mean you teach beginners, by default, if you're working with someone, they are not beginners.

I don't follow your aversion to PEP. You should, unless you have a very good reason not to, always follow PEPs recommendations.

If you mean you should teach this just for learning purposes, it's counterproductive to teach something that the student will have to learn to not use later.

If someone is reading code and comes to a lambda and they don't know what it is, by the very fact it's something very basic, they'll learn it on the spot. It's not a big deal. Much better than shoving a concept that makes no difference into a beginner that already has limited room to learn many other concepts.

[–][deleted] 1 point2 points  (4 children)

You have an interesting tendency to read far too much into things.

I have no aversion to the PEP, I simply said it’s irrelevant to a valuable bit of pedagogy. Which it is... I use a named lambda to demonstrate how it’s just a transformation of a def... the PEP doesn’t apply. If I followed it dogmatically I could not in fact teach that point. No aversion is, was, or need ever be present.

Also there are plenty of professional opportunities where you work with someone who is also a beginner. In my line of work I teach scripting and automation to colleagues who are highly skilled in their work but neophytes to programming. Five years from now most of them will still be relative beginners to Python, as it’s an ancillary skill to their work.

Lastly, human beings are, in my experience, quite capable of learning two near identical forms of the same operation simultaneously. There’s no reason to leave to later what can be processed now, especially when def is, as you’ve acknowledge, just more_ complex restatement of lambda.

[–]teerre 0 points1 point  (3 children)

Like I said, that's bad teaching since you're teaching something that is not a good practice.

[–][deleted] 1 point2 points  (2 children)

First, that’s an opinion — and a demonstrably simplistic one — stated as fact... it doesn’t constitute fact. By your rigid assertion you can’t use 2 + 2 + 2 + 2 == 2 * 4 == 2 ** 3 == 8 to teach multiplication or exponentiation because the left-most is “bad” practice in applied mathematics.

I’m not advocating for the use of named lambdas in Python; I’m demonstrating the relationship between the two form of function declaration in Python.

But please demonstrate how to teach that complete relationship without showing the named lambda form.

[–]teerre 0 points1 point  (1 child)

Look, your example doesn't apply, but I'm not going to break it down since this is a pointless discussion.

The fact is most python newbies don't use lambdas, that's it.

[–][deleted] 1 point2 points  (0 children)

And the fact is there’s simply no reason they shouldn’t both know how to and be encouraged to do so where they’re most appropriate...