+2

why lambda, and use cases

So admittedly, I am a newb to python and trying to work thru the course on SL. I have a fair amount of shell, and HTML/CSS/Javascript hack-n-slashing experience, but developing is definitely not my forte, nor have I ever had any formal studies. Chose to learn python to upgrade my skill set, and see if I might want to become a developer. with all that said... I am getting comfortable with writing some basic code from scratch, and came across 'lambda' in the 'functional programming' module. I am trying to figure out why I would use one syntax over another. I get the the code size and memory savings by using lambda, but can't wrap my head around why I would sacrifice readability, or what the point is between the 'lambda 1' vs 'lambda 2' scenarios below: ----- # named function def squareNum(num): result = num ** 2 return result print(squareNum(4)) # lambda 1 double = (lambda x: x ** 2) print(double(4)) # lambda 2 double = ((lambda x: x ** 2) (4)) print(double) ----- Any thoughts / discussion is welcome. I plan to do some googling, but I thought this was an interesting enough question that other newbies could benefit. ;)

2/14/2020 8:27:05 AM

Code Crasher

4 Answers

New Answer

+6

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.

+2

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.

+1

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!

0

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!