Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Lambda function Uchikago Silly Frenchman Posts: 20 Threads: 14 Joined: Jun 2019 Reputation: 0 Likes received: 0 #1 Jul-16-2019, 02:24 AM Hi, i'm a bit confused with this code ```x=10 x=lambda : x+2 print(x())```because the right side of the '=' is evaluated first so this should have returned 12 rather than reported an error as it did error: ``` x=lambda : x+2 TypeError: unsupported operand type(s) for +: 'function' and 'int'```Please explain why this code doesn't work Gribouillis Posts: 2,051 Threads: 18 Joined: Jan 2018 Reputation: 185 Likes received: 489 #2 Jul-16-2019, 04:54 AM (This post was last modified: Jul-16-2019, 04:54 AM by Gribouillis. Edited 2 times in total.) After the two first lines, x is no longer an integer, it is an anonymous function ```>>> x = 10 >>> x = lambda: x + 2 >>> x at 0x7fb32e167048> ```At this stage, `x + 2` has not yet been executed. It's only executed when you call `x()` but then it is an error because you're trying to add a function and an integer ```>>> x() Traceback (most recent call last): File "", line 1, in File "", line 1, in TypeError: unsupported operand type(s) for +: 'function' and 'int' ```See this other example which is a regular function but works the same ```>>> y = 122 >>> def spam(): ... print('The value of y is', y) ... >>> y = 314 >>> spam() The value of y is 314 ``` millpond Silly Frenchman Posts: 38 Threads: 11 Joined: Jul 2019 Reputation: 1 Likes received: 5 #3 Jul-16-2019, 05:17 AM It appears that the problem is that line 2 assigns the return of a function to its own variable. ```x=10 y = (lambda x : x+2 ) print(y(x)) ```Works, perfringo Da Bishop Posts: 1,139 Threads: 5 Joined: Jun 2018 Reputation: 117 Likes received: 256 #4 Jul-16-2019, 08:56 AM Just a gentle reminder - quote from PEP 8 -- Style Guide for Python Code >>> Programming Recommendations Quote:Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier. Yes: `def f(x): return 2*x` No: `f = lambda x: 2*x` The first form means that the name of the resulting function object is specifically 'f' instead of the generic ''. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression) I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Help with AWS Lambda function faws2019 0 136 Aug-27-2019, 01:54 PM Last Post: faws2019 eval lambda function with restricted context Olivier 7 639 Mar-04-2019, 10:45 PM Last Post: Olivier Using a lambda function within another function JChapman 8 879 Jan-08-2019, 01:54 PM Last Post: JChapman Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil anandmn85 2 1,138 Apr-19-2018, 05:56 AM Last Post: anandmn85 How do I make an assignment inside lambda function? Standard_user 2 9,898 Nov-13-2016, 05:54 PM Last Post: Standard_user lambda function tkj80 3 2,120 Oct-24-2016, 06:27 PM Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)