Noob question: Confusing lambda!

This is the place for queries that don't fit in any of the other categories.

Noob question: Confusing lambda!

Postby XndrK » Sat Jun 15, 2013 8:21 pm

I don't really understand lambdas. I am reading Python 2.7's built-in tutorial, and I came across this. (It's running in the interpreter, by the way.)

Code: Select all
>>> def make_incrementor(n):
...     return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43


How does the x come in to the f part? Is there some sort of metaprocess that I'm missing?

My best guess is that when you define the function f as make_incrementor(42), you run the lambda as f. Is that right?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Noob question: Confusing lambda!

Postby Yoriz » Sat Jun 15, 2013 8:30 pm

Code: Select all
def make_incrementor(n):
    return lambda x: x + n

is the same as doing
Code: Select all
def make_incrementor2(n):
    def inner(x):
        return x + n
    return inner

Code: Select all
def make_incrementor(n):
    return lambda x: x + n

def make_incrementor2(n):
    def inner(x):
        return x + n
    return inner

f = make_incrementor(42)
f2 = make_incrementor2(42)
print f(0), f2(0)
print f(1), f2(1)
42 42
43 43


ps Sorry but i read your signature
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Noob question: Confusing lambda!

Postby XndrK » Fri Jul 19, 2013 2:10 am

So how exactly does the calling work? I sort of get it, but it helps when someone explains it to me.

Not to mention, why would you want to nest functions like that?

Also, I've been getting some confusion about the anonymous functions. I'm writing this:

Code: Select all
def add_up(x, n):
    return lambda: x + n


and it's returning:
Code: Select all
<function <lambda> at 0x02395AF0>


So, what I did is I built another "inner"-style function:

Code: Select all
def add_up(x, n):
    def inner():
        return x + n


and it returned this.

Code: Select all


I know this doesn't really have to do with anonymous functions proper, but how do you insert global variables into local functions?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm

Re: Noob question: Confusing lambda!

Postby Yoriz » Fri Jul 19, 2013 12:39 pm

Code: Select all
def add_up(x, n):
    return lambda: x + n

lab = add_up(23, 19)
print lab

Output
Code: Select all
<function <lambda> at 0x013575B0>

You need to call the returned lambda.
Code: Select all
def add_up(x, n):
    return lambda: x + n

lab = add_up(23, 19)
print lab()

Output
Code: Select all
42


Code: Select all
def add_up2(x, n):
    def inner():
        return x + n

lab = add_up2(23, 19)
print lab

Output
Code: Select all
None

You need to return inner from add_up2 and then call the returned inner function.
Code: Select all
def add_up2(x, n):
    def inner():
        return x + n
    return inner

lab = add_up2(23, 19)
print lab()

Output
Code: Select all
42
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Noob question: Confusing lambda!

Postby XndrK » Mon Jul 29, 2013 1:29 am

Oh...btw, I'm using 2.7. Are there any "Hey, you know 2.7, now for 3.3!" sort of tutorials?
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], W3C [Linkcheck] and 3 guests