Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #93164 > unrolled thread
| Started by | fl <rxjwg98@gmail.com> |
|---|---|
| First post | 2015-06-25 13:53 -0700 |
| Last post | 2015-06-25 15:22 -0600 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
Could you give me the detail process of 'make_incrementor(22)(33)'? fl <rxjwg98@gmail.com> - 2015-06-25 13:53 -0700
Re: Could you give me the detail process of 'make_incrementor(22)(33)'? Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-25 15:22 -0600
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-06-25 13:53 -0700 |
| Subject | Could you give me the detail process of 'make_incrementor(22)(33)'? |
| Message-ID | <20661fcb-7773-4f17-bbfa-8533914e637f@googlegroups.com> |
Hi, I read a tutorial on lambda on line. I don't think that I am clear about the last line in its example code. It gives two parameters (22, 23). Is 22 for n, and 23 for x? Or, it creates two functions first. Then, each function gets 22 while the other function gets 23? Please help me on this interesting problem. Thanks, >>> def make_incrementor (n): return lambda x: x + n >>> >>> f = make_incrementor(2) >>> g = make_incrementor(6) >>> >>> print f(42), g(42) 44 48 >>> >>> print make_incrementor(22)(33)
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2015-06-25 15:22 -0600 |
| Message-ID | <mailman.84.1435267411.3674.python-list@python.org> |
| In reply to | #93164 |
On Thu, Jun 25, 2015 at 2:53 PM, fl <rxjwg98@gmail.com> wrote: > Hi, > > I read a tutorial on lambda on line. I don't think that I am clear about > the last line in its example code. It gives two parameters (22, 23). > Is 22 for n, and 23 for x? Or, it creates two functions first. Then, > each function gets 22 while the other function gets 23? > > >>>> def make_incrementor (n): return lambda x: x + n >>>> >>>> f = make_incrementor(2) >>>> g = make_incrementor(6) >>>> >>>> print f(42), g(42) > 44 48 >>>> >>>> print make_incrementor(22)(33) make_incrementor is a function that takes an argument n. It returns a function that takes an argument x. So when you do make_incrementor(22) that passes 22 to make_incrementor as the value of n, and when you do make_incrementor(22)(33), that also passes 22 to make_incrementor as the value of n, and then it passes 33 to the returned function as the value of x.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web