lambda functions ?

Don Morrison donmorrison at gmail.com
Mon Feb 5 17:16:02 EST 2007


Maybe you would like a generator:

>>> def f(n):
...     while True:
...             n += 1
...             yield n
...
>>> a = f(5)
>>>
>>> a.next()
6
>>> a.next()
7
>>> a.next()
8
>>> a.next()
9
>>>


On 2/5/07, Maxim Veksler <hq4ever at gmail.com> wrote:
> Hello,
> I'm new on this list and in python.
>
> It seems python has some interesting concept of "ad hoc" function
> which I'm trying to understand without much success.
>
> Take the following code for example:
>
> """
> >>> def make_incrementor(n):
> ... return lambda x: x + n
> ...
> >>> f = make_incrementor(42)
> >>> f(0)
> 42
> >>> f(1)
> 43
> """
>
> I really don't understand whats going on here.
> On the first instantiating of the object "f" where does "x" gets it's
> value? Or is it evaluated as 0? ie "x: 0 + 42"
>
> And what is the "f" object? An integer? a pointer? an Object?
> I'm coming from the C world...
>
> Could some please try (if even possible) to implement the above code
> without using "lambda" I believe it would help me grasp this a bit
> faster then.
>
> Thank you,
> Maxim.
>
>
> --
> Cheers,
> Maxim Veksler
>
> "Free as in Freedom" - Do u GNU ?
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list