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

Ian Kelly ian.g.kelly at gmail.com
Thu Jun 25 17:22:45 EDT 2015


On Thu, Jun 25, 2015 at 2:53 PM, fl <rxjwg98 at 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.



More information about the Python-list mailing list