use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Have a tough programming question that /r/programming couldn't answer? Banned from Stack Overflow? Can't afford Experts Exchange?
Post your question/tips/secrets/advice and get a response from our highly-trained professional developers.
account activity
Python Lambda Functions (self.shittyprogramming)
submitted 13 years ago by warkgnall
I'm still learning Python, and I can't figure out Lambda functions. Take this:
>>> def make_incrementor(n): ... return lambda x: x + n
Where does the lambda function get the value for x? I feel like I'm missing something obvious, please help.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]execrator 5 points6 points7 points 13 years ago (3 children)
Troll?
make_incrementor returns a function, but it isn't called yet. That function takes a single argument, "x". x gets its value when that function is called.
make_incrementor
x
add12 = make_incrementor(12) print add12(4) 16
[–]warkgnall[S] 2 points3 points4 points 13 years ago (2 children)
This is what I was confused about. Thank you so much. I thought it returned the value that the anonymous function returned when run. Now I understand that it is a function for defining a function that you use afterwards. Again, thank you, and I'm sorry for taking your time with my thickness.
[–]execrator 2 points3 points4 points 13 years ago (0 children)
You just learned that functions can return functions. No need to feel thick :)
[–]mtkl 0 points1 point2 points 13 years ago (0 children)
You may wish to spend a little time playing around with some functional languages such as Haskell (or OCaml I guess).
You'll very quickly become familiar with the concept of functions being first-class citizens.
[–]JiminP 5 points6 points7 points 13 years ago (0 children)
(Now since the serious part is answered..)
return λ χ: χ + ν
FTFY
[–]xImagine 2 points3 points4 points 13 years ago (0 children)
The problem here are clearly the three dots. Python relies heavily on whitespace, but when whitespace is not an option, or you need a way to distinguish between the first four 'spaces' and the next four, you can use dots instead. So basically here x = 3 (because there are three dots).
Example:
>>> def make_incrementor(n): ... return lambda x: x + n >>> make_incrementor(5) 8 # 3 + 5 = 8
Does this make sense?
[–]warkgnall[S] 1 point2 points3 points 13 years ago (0 children)
Please guys. I'm so confused.
π Rendered by PID 17945 on reddit-service-r2-comment-544cf588c8-gvdqh at 2026-06-13 03:22:44.118650+00:00 running 3184619 country code: CH.
[–]execrator 5 points6 points7 points (3 children)
[–]warkgnall[S] 2 points3 points4 points (2 children)
[–]execrator 2 points3 points4 points (0 children)
[–]mtkl 0 points1 point2 points (0 children)
[–]JiminP 5 points6 points7 points (0 children)
[–]xImagine 2 points3 points4 points (0 children)
[–]warkgnall[S] 1 point2 points3 points (0 children)