[Solved] why lambda, and use cases
2/14/2020 8:27:05 AMKode Krasher
8 AnswersNew Answer
[Part 1 of 4] Code Crasher Excellent question and well done setting up the context for what you are trying to reconcile in your understanding. I also dig your approach to following up to share your discoveries. Nice job and thanks for taking the time to help others who hopefully stumble upon this thread. My response to your question is as follows: Ultimately, lambdas are an artifact of functional programming. However, due to limited support for functional programming in Python, the usage of lambdas are a bit awkwardly implemented into the language and consequently, can be less preferred to alternative approaches available in Python. Guido van Rossum provides a lot of his personal insight and context from his perspective regarding support in Python for functional programming in the link below: https://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html?m=1 (continued...)
If u need a function only once eg for filtering or mapping, it is a shortcut and keeps the flow. If u need to square a number often, giving a name to the func is the better way.
[Part 2 of 4] After reviewing the links you posted, I wasn't satisfied with the content as a reference for learning about "the when and why" to use lambdas in Python. Some seemed to use examples that should be avoided. The Stackoverflow link does highlight a limitation of lambdas in Python, which, in my opinion, is due to some poor language design decisions. I pulled a set of links that I'd suggest on this topic which you may prefer as well. There may be some overlap in these articles. However, each one is worth reviewing as they collectively reinforce many strong talking points I agree with. The significance of some talking points may not register to less experienced programmers for a while. But there are quite a lot of great points made that many will connect with. https://stackabuse.com/functional-programming-in-python/ https://realpython.com/python-lambda/ https://treyhunner.com/2018/09/stop-writing-lambda-expressions/ (continued...)
[Part 4 of 4] Back to Python, if you would like to see some sample code using lambdas compared to for-loop and list compression alternatives, check out the code series I put together in the links below. Experimenting with For Loops vs Lambdas vs List Comprehensions in Python: #1: Using for-loop. - https://code.sololearn.com/cXLf85iK5T4U/#py #2: Using lambdas with calling print() within nested lambda. - https://code.sololearn.com/ca6pN64a79Tx/#py #3: Using lambdas and calling print() function once. - https://code.sololearn.com/cg5H878BmTo5/#py #4: Using List Comprehensions - https://code.sololearn.com/ckl4rbtLFm7h/#py
Code Crasher lambdas are trivial? 😂😂😂😂😂😂😂😂😂😂 maybe you get a better insight by learning filter/map functions. They are very related to lambdas and absolutely useful once you got familiar with them.
Not sure if I am anywhere closer to my answer, but googling made me start to see I can do some useful things in python now, that were only available to me with bash. So if nothing else, I broke thru being given projects to try, to actually thinking of ways to create projects for myself. A little confidence maybe? ----- urlList = ['http://yahoo.com', 'http://sololearn.com', 'http://bing.com', 'http://aol.com', 'http://google.com'] wwwList = [urls.replace('http://', 'https://www.') for urls in urlList] print(wwwList) fullList = list(map(lambda www: str.replace(www, 'http://', 'https://www.'), urlList)) print(fullList) ----- Having multiple ways of doing things never helps my comprehension, but I have ran across a few examples that got the gears turning. I think a parsing utility in python to fix JSON data is in my near future. ;) @Oma Falk -- Thank you for the reply... I am humbled you would spend the time to explain something that is probably extremely trivial for you. For the short while I have been on SL, I have seen your name all over the place, and while I don't understand most of your code, I have thoroughly enjoyed your code examples. Thank you for all your contributions! /me passes Oma Falk a cold one! Cheers!
So I guess we can apply the old Idiom... "the more I learn, the less I know." -Unknown OR if you prefer: "Wisest is he that knows he does not know" -Socrates I have been learning long enough to know this is the Truth! Anyway, here are some links from my journey down the rabbit hole... A good place to start was the official docs.python.org site... concise, and to the point: https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions This is a site I go to a lot for syntax on multiple languages, and it has a web based shell to experiment in: https://www.w3schools.com/python/python_lambda.asp Another brief explanation with examples: https://book.pythontips.com/en/latest/lambdas.html This was a great read, and I think combined with the next link turned on a few light bulbs: https://www.makeuseof.com/tag/python-lambda-functions/ This is probably more advanced, but it is a great article with a lot of examples, and covers the most variety of use cases: https://thispointer.com/python-how-to-use-if-else-elif-in-lambda-functions/ This seems to be the most complete article I found... To be honest, I have not gotten thru it. It will be a weekend read for me, I think: https://www.afternerd.com/blog/python-lambdas/#what-is-python-lambda And lastly, this is just a thread with a "Gotcha" that someone needed help with, but I found it most interesting: https://stackoverflow.com/questions/452610/how-do-i-create-a-list-of-python-lambdas-in-a-list-comprehension-for-loop I have searched the Code Playground for 'lambda' and looked at some of the code, but it seems to be pasted from lessons on SoloLearn or rehashing other's code... If anyone wants to share links to their code in the Code Playground using lambdas, I personally would enjoy studying them. Please, Share a link in this thread. (I am sure other's will benefit as well.) Have I mastered this? No, but it has been enlightening! I hope others find these links useful. Cheers!