understanding the return type of anonymous function lambda

2024/11/17 1:33:17

I am trying to understand how can lambda function be used.

def adder_func(a, b):return a + bprint(adder_func(4, 5))# trying with lambda
print(list(lambda a, b: a + b))

When trying to use lambda as a adder_function, why is that the result cannot be printed?

O/P:

9
Traceback (most recent call last):File "C:\Users\redsuren\PycharmProjects\py_charm_tutorial_amigoscode\lambda_adder.py", line 7, in <module>print(list(lambda a, b: a + b))
TypeError: 'function' object is not iterableProcess finished with exit code 1

AND, If keyword "list" is removed and try to print the return value, it give me some hexa_value, what does this value represent?

print(lambda a, b: a + b)

O/P:

<function <lambda> at 0x0000019440B5E0D0>
Answer

Seems like you're trying to do this

adder_func = lambda a, b: a + b
print(adder_func(4, 5))

Or this, which defines the function, then passes the parameters on the same line... You don't need list() to do that

a = 4
b = 5
print((lambda a, b: a + b)(a, b))

Printing a function object isn't all that useful, as you can see, and the list() function accepts iterables, which function objects are not (not to be confused with the return values of functions that are iterable, or generator objects), for example

def adder_func(a, b):yield a + b
print(adder_func)  # <function adder_func at 0x1019c3b80>
print(adder_func(4, 5))  # <generator object adder_func at 0x101996200>
print(list(adder_func(4, 5)))  # [9]
https://en.xdnf.cn/q/118860.html

Related Q&A

What is the meaning of Failed building wheel for flask-mysqldb in pip3 install?

I have a MacBook Air with macOs Sonoma 14.0 and when I write in the terminal $ pip3 install flask-mysqldbI get the error:How can I fix this?

How do I install pygame for a new version of idle? (Windows) [duplicate]

This question already has answers here:Error on install Pygame(3.9) install (Win10) [duplicate](1 answer)Unable to install pygame on Python via pip (Windows 10)(6 answers)Closed 3 years ago.I installed…

How to parse a dynamic dom element?

I want to make a parser for scraping price, however I cant find the working method of parsing innerHTMLI dont know why, but selenium (getAttribute(innerHTML)), phantomjs (page.evaluation function(){ret…

Get a string in Shell/Python with subprocess

After this topic Get a string in Shell/Python using sys.argv , I need to change my code, I need to use a subprocess in a main.py with this function :def download_several_apps(self):subproc_two = subpro…

Python Indentation Error when there is no indent error [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 7 months ago.Is it me or the interpreter? I see no indentation error in my cod…

How to mark rgb colors on a colorwheel in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 months ago.Improv…

Cant get Selenium to loop through two dialogue box options correctly

So basically: the goal is to click on each symbol for each sector on this website, that pops up a table with contact details, I want to copy all of that information and store it in a file. Right now ev…

Is it possible to use a JSON Web Token/JWT in a pip.conf file?

Im trying to make it possible for my application to fetch a package from a private feed in Azure DevOps using pip and a pip.conf file. I dont want to use a PAT for obvious reasons, so Ive created a ser…

sqlite3.Cursor object has no attribute __getitem__ Error in Python Flask

This is my code. I get this error everytime I press login:sqlite3.Cursor object has no attribute __getitem__This is my login tab:@app.route(/, methods=[GET, POST]) def login():error= Noneif request.met…

Merge Sort Implementation Check

I am doubtful of my implementation of the merge sort for two cases specifically:1. If the size of the list is 2, then I have swapped the values if they are not in the ascending order else I have return…