Python Forum
Why my lambda doesn't work properly?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why my lambda doesn't work properly?
#1
I thought it should give 2 as passed argument is 1, so 1+1 should be done. But it gives me 1.

>>> def func(arg=lambda i: i+1):
print(arg)


>>> func(1)
1
Reply
#2
I don't know why you'd expect anything else. You're setting the default value of the parameter arg to a lambda (why?), but when you call the function, you're passing the value 1, so the default isn't used.

What are you actually trying to achieve?
Reply
#3
I tried to understand this part from Python tutorial about passing lambdas as an argument.

4.7.6. Lambda Expressions
...

The above example uses a lambda expression to return a function. Another use is to pass a small function as an argument:
>>>

>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1])
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
Reply
#4
Remember that a lambda is a function. What do you do with functions? You call them of course. So the sort method calls the function passed as key on each pair to decide what to sort by (the second element in pair, pair[1]).

Perhaps it's useful to consider the function map that takes a list of values and a function to apply to each value, producing a new list. map exists in the standard library, so I'm calling my implementation below my_map:

>>> def my_map(f, values):
...     new_values = []
...     for v in values:
...             new_values.append(f(v))
...     return new_values
... 
>>> my_map(lambda v: v * 2, [1, 2, 3])
[2, 4, 6]
>>> my_map(lambda v: v.upper(), ["foo", "bar", "baz"])
['FOO', 'BAR', 'BAZ']
>>> 
my_map loops over each of the items in values, calling the function f on them (line 4) and putting those values in a new list which is returned. On line 7, I pass a lambda that just doubles its argument, so when my_map applies that to the items in [1, 2, 3], you get the list [2, 4, 6]. It's a similar thing with the second example on line 9.
Reply
#5
Thank you very much, I got it now.
Reply
#6
Another typical example is the function filter that keeps items for which a predicate function returns true. For example,

>>> my_filter(lambda v: v % 2 == 0, [1, 2, 3, 4, 5, 6])
[2, 4, 6]
>>> my_filter(lambda v: len(v) > 3, ["c++", "python", "c", "awk", "java"])
['python', 'java']
It is left as an exercise for you to implement my_filter ;).
Reply
#7
Thanks, ndc85430.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 870 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  While Loop Does Not Work Properly mactron 4 871 Jun-22-2023, 01:04 AM
Last Post: mactron
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,679 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 838 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,655 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,048 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,928 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,657 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  Process doesn't work but Thread work ! mr_byte31 4 2,552 Oct-18-2021, 06:29 PM
Last Post: mr_byte31
  Psycopg2 doesn't work with python2 MedianykEugene 3 2,887 Aug-10-2021, 07:00 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020