Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python realize that only one of the return values of a function is taken?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how python realizes the function return value to take only one of them". The editor shows you the operation process through the actual case, the operation method is simple and fast, and the practicality is strong. I hope that this article "how to realize the python function return value to take only one of them" can help you solve the problem.

Take a look at the code ~ def test (): return 1Power2a, b = test () 12a, _ = test () 1 # indicates that what is returned in a, _ = test () is an int type that can be operated directly # a, b = test () the return value is a tuple, and the error is reported directly:

The decorator implements a function timer to compare the operation speed of the for loop with the map, anonymous function.

Question:

What if the decorated function has a return value?

Program content: import timeimport randomimport stringimport functools# problem: the decorated function has the return value li = [random.choice (string.ascii_letters) for i in range (100)] def timeit (fun): @ functools.wraps (fun) # in order to retain the function name and help document information of the decorated function def wrapper (* args * * kwargs): "this is a wrapper function" start_time = time.time () res = fun (* args " * * kwargs) end_time = time.time () print ('run time is:% .6f'% (end_time-start_time)) return res return wrapper@timeitdef fun_list (n): "this is the fun_list function" return [I * 2 for i in range (n)] @ timeitdef fun_map (n): "this is the fun_map function" return list (map (lambda x:x*2) " Range (n)) print (fun_list (5000)) print (fun_map (5000)) on "how python implements that only one of the return values of a function can be taken" is here. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report