Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #93166

Re: Could you give me the detail process of 'make_incrementor(22)(33)'?

References <20661fcb-7773-4f17-bbfa-8533914e637f@googlegroups.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2015-06-25 15:22 -0600
Subject Re: Could you give me the detail process of 'make_incrementor(22)(33)'?
Newsgroups comp.lang.python
Message-ID <mailman.84.1435267411.3674.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web