the-generalist.com

  • Increase font size
  • Default font size
  • Decrease font size
joomla templates, wordpress themes, drupal, datalife engine, graphics, seo,
Home

Python Recursive Lambda Function Example

E-mail Print PDF

A recursive lambda function in Python is something you are probably never going to have to do. You should probably look into other solutions first, but if you're curious like me you may want to try it. I ran into a situation where I was looping through objects at runtime and creating lambda functions as a wrapper function with my object as the default value. In my case I was linking a bunch of function calls to menus in PyQt. I wanted to connect a menu option to a function that ran all of the other menu options.

I decided to try to write a lambda function that worked similarly to a recursive function and worked back through the list of lambda functions that were created. It probably would have been better to just create a list of the lambda functions to call, but I wanted to see if I could make it work and keep all the code for the menus in the same place. I ended up with something similar to the code below. This gave me a function that would loop through and call all the previous lambda functions created in my loop.

def foo(x):
return x**2
 
r = lambda x = None: x
for i in range(10):
    f = lambda x = i:foo(x)
    r = lambda x = r, y = f:(y(), x())
 
print r()

In this case I've made a function foo that just squares a value and a for loop that will create ten lambda functions f() that each call this function for a different value of x. r() is my recursive function. The first r (r = lambda x = None: x) just returns None. I need this because all later iterations of r() call the previous r(). This could be done another way by putting an if statement in the r() lambda; I think this is more readable as an example though. All the other r() that are created in the group call the current f() and then call the previous r(). That's how the recursion happens. So in the end calling r() calls all the f()s that were created.

Last Updated on Thursday, 21 July 2011 23:06  

Comments  

 
0 #13 FirstGustavo 2017-11-19 20:06
I see you don't monetize your website, don't waste your traffic, you can earn extra bucks every month because you've got hi quality content.
If you want to know how to make extra bucks, search for:
Boorfe's tips best adsense alternative
Quote
 
 
0 #12 LionelX 2017-09-19 12:36
I have noticed that your page needs some fresh posts.
Writing manually takes a lot of time, but there is tool
for this time consuming task, search for; Wrastain's tools for content
Quote
 
 
0 #11 MeganX 2017-09-19 09:35
I have noticed that your website needs some fresh articles.
Writing manually takes a lot of time, but there is tool for this boring task, search for; Wrastain's tools for content
Quote
 
 
0 #10 Anemonalove 2017-07-17 02:51
Hello fellas! Who wants to meet me? I have profile at HotBabesCams.com, we can chat,
you can watch me live for free, my nickname is Anemonalove , here is my
pic:

https://3.bp.blogspot.com/-u5pGYuGNsSo/WVixiO8RBUI/AAAAAAAAAFA/JWa2LHHFI2AkHParQa3fwwHhVijolmq8QCLcBGAs/s1600/hottest%2Bwebcam%2Bgirl%2B-%2BAnemonalove.jpg
Quote
 
 
0 #9 xujiansg.com 2017-07-03 12:59
Hey there! I just wanted to ask if you ever have any issues
with hackers? My last blog (wordpress) was hacked
and I ended up losing months of hard work due to no data backup.
Do you have any methods to stop hackers?
Quote
 
 
0 #8 business Cards 2016-05-24 10:16
hi!,I love your writing very much! proportion we be in contact
more approximately your post on AOL? I require
a specialist in this space to uunravel my problem.
Maybe that iis you! Looking ahead to look you.
Quote
 
 
0 #7 clash of clans 2015-06-21 20:04
The even more sets of walls you can find, the much more likely it
is to avoid your enemy when they are attacking.



my weblog :: clash of clans strategy attack town hall
level 7: http://telechargerfarcry4.fr
Quote
 
 
0 #6 bridgettdabney65. 2015-05-31 23:51
Howdy! I know this is kind of off topic but I was wondering
if you knew where I could find a captcha plugin for my comment form?
I'm using the same blog platform as yours and I'm having problems
finding one? Thanks a lot!

Feel free to surf to my site: free natural male enhancement exercises (bridgettdabney 65.soup.io: http://bridgettdabney65.soup.io/?sessid=112ca3df0b8115eb6e8434d5fea1d9a2)
Quote
 
 
0 #5 how to hack clash 2015-05-02 06:45
Nous venons de mettre à jour notre Clash of clans hack
conception avec une nouvelle marque.
Quote
 
 
0 #4 clash of clans free 2015-04-30 04:26
Totally free Gems, Gold, and Elixir provides you with the chance to save
hundreds now that you won't have to get these products ever
again!
Quote
 

Add comment


Security code
Refresh

Main Menu