Discussion:
[Tutor] Using lambda
rakesh sharma
2015-08-24 06:38:06 UTC
Permalink
I am beginner in pythonI see the use of lambda has been for really simple ones as in the numerous examples over the net.Why cant we use lambda in another one like g = lambda x: (lambda y: y + 1) + 1when I am able to do that in two lines
h = lambda x: x + 1>>> h(12)13y = lambda x: h(x) + 1>>> y(1)3

thanksrakesh
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Cameron Simpson
2015-08-24 07:35:43 UTC
Permalink
Post by rakesh sharma
I am beginner in pythonI see the use of lambda has been for really
simple ones as in the numerous examples over the net.Why cant we
use lambda in another one like
g = lambda x: (lambda y: y + 1) + 1
when I am able to do that in two lines
Post by rakesh sharma
h = lambda x: x + 1
h(12)
13
Post by rakesh sharma
y = lambda x: h(x) + 1
y(1)
3
Hi,

First, please include more whitespace in your posts to make them easier to
read. If you are includining line breaks etc, I think something is eating them.

Regarding your question, you can do what you ask. You suggestion was:

g = lambda x: (lambda y: y + 1) + 1

The thing you've missed is that a lambda is a function; you need to call it.
Your example only defines a lambda but doesn't call it. Try this:

g = lambda x: (lambda y: y + 1)(x) + 1
Post by rakesh sharma
g(12)
14

So yes, you can do it. But as you can see it doesn't make for very readable
code. Lambdas are ok for small expressions. When things get more complex it is
worth breaking them up.

Cheers,
Cameron Simpson <***@zip.com.au>
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-24 08:11:33 UTC
Permalink
Post by rakesh sharma
I am beginner in pythonI see the use of lambda has been for really simple ones as in the numerous examples over the net.Why cant we use lambda in another one like g = lambda x: (lambda y: y + 1) + 1when I am able to do that in two lines
h = lambda x: x + 1>>> h(12)13y = lambda x: h(x) + 1>>> y(1)3
Your email has arrived with no formatting so its hard to work
out your examples.

You can use lambda in multiple lines but it is limited
to a single expression.
Post by rakesh sharma
ml = lambda n: (
... 5*n + (
... n*n - (
... 25)))
Post by rakesh sharma
ml(7)
59
Post by rakesh sharma
adder = lambda x: lambda n: n+x
adder(3)
<function <lambda> at 0x7fe3dca0ccf8>
Post by rakesh sharma
add3 = adder(3)
add3(5)
8
But for longer functions you are better using def to define them.
In Python lambda is not the underlying mechanism for creating
functions it is a convenience feature. Python is not Lisp.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...