r/learnpython • u/Nythious • Feb 20 '21
What are you having difficulty learning or are currently struggling to learn?
I'm curious what the community here has difficulty with when it comes to learning Python. Partly because I want to see if the more experienced community here (or myself) can help point you into the right direction. . . but also because I'm trying to come up with video tutorial ideas for future content and would love to hear from Python learners what would be most useful to dedicate our time on.
I'm looking forward to hearing what you guys have to say!
417
Upvotes
3
u/remuladgryta Feb 21 '21 edited Feb 21 '21
Lambdas are a concept borrowed from functional (as in mathematical function) programming. Programmers who write in functional programming languages love when they spot a pattern in their program because then they get to write a general version of the pattern and never worry about writing similar code again.
An example is one time long ago a functional programmer noticed that they had a lot of functions that took a list as a parameter and returned a new list where each element of the result was based on each element of the parameter.
This made the functional programmer very excited. "Look, all these functions do exactly the same thing except for one little part! I bet I can write a general list transforming function." the functional programmer thought. And so they did:
Now that they had a general version of their function, they could rewrite their old ones like this:
But wait, that's kinda cluttered. Now you have all these functions
square,add_ten, andhalvethat have names even though you only care about them in the context of their corresponding blah_list function. You could move the helper functions into the function bodies of the "real" functions to hide some of that clutter from the rest of the program like so:That's a bit better, but what's the point of declaring a function and giving it a name if the sole reason for that function existing is to be a behavioral parameter to some other function? That's the problem lambdas aim to help with. Lambdas are "throwaway" functions without a name and more concise notation, perfect for using smack dab in the middle of a function parameter list. The same list functions written using lambdas:
I've been calling the general function
list_transformall this time, but in reality there is an even more general version in the python standard library calledmap.mapbehaves similarly tolist_transformexcept it returns an iterator instead of a list and it can take more than just one iterable (lists are an example of iterable objects) as input. For example if you have two listsaandband you want to add them together element-wise you could write something like thisbut with
mapyou could instead write it like thismapis a useful function, but in python it is often more natural to write these kinds of transformations as list comprehensions:However, there are tons of useful functions that take another function as one of their parameters to turn a general pattern into a specific one or to slightly modify a default behavior. Examples include but are not limited to the
sortedfunction, thefunctoolsmodule, and theitertoolsmodule.