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 to define functions in Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly shows you "how to define functions in Python", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to define functions in Python" this article.

There are two ways to define a function in Python, one is to use the normal way of def definition, the other is to specify the name of the function, the second is to use lambda definition, do not need to specify the name, called the Lambda function.

Lambda function is also called anonymous function. Anonymous function is a function without a name. A function without a name is OK. Of course you can. If some functions are only temporary and their business logic is simple, there is no need to give them a name.

For example, the extras in the movie often play a small part, and at most they set off the lead actor and walk-on. Do they need a name? No, because they only appear on camera temporarily, they may not need it next time, so don't bother to make up a number and give each of them a name. After all, it takes a lot of effort to have an elegant name.

Let's start with a simple lambda function.

> lambda x, y: X roomy

X and y are the two parameters of the function, and the expression after the colon is the return value of the function. You can see at a glance that this function is finding the sum of two variables, but as a function, how to use it without a name? Here, let's bind a name to the anonymous function, which makes it possible for us to call the anonymous function.

> add = lambda x, y: X

> add

> add (1Pol 2)

three

It is equivalent to a regular function.

> def add2 (x, y):

... Return Xeroy

...

> add2

> add2 (1Pol 2)

three

If you define an anonymous function and bind it with a name, it's a bit of an icing on the cake, usually using the lambda function directly. So where is the correct use of the lamdba function?

1. Functional programming

Although Python is not a pure functional programming language, it provides many features of functional programming. Functions such as map, reduce, filter and sorted all support functions as parameters, and lambda functions can be used in functional programming.

Please take a look at the question: a list of integers that are required to be sorted in ascending order according to the absolute values of the elements in the list. What would you do? Think for a minute and look down.

> list1 >

> sorted (list1, key=lambda x: abs (x))

[0, 1,-2, 3,-4, 5,-6]

The sorting function sorted supports receiving a function as a parameter, which is used as the sorting basis for sorted. Here, it is sorted by the absolute value of the list elements. Of course, I can also use ordinary functions to achieve this:

> > def foo (x):

... Return abs (x)

...

> sorted (list1, key=foo)

[0, 1,-2, 3,-4, 5,-6]

It's just that the code doesn't look Pythonic enough in this way.

2. Closure

Closure itself is an obscure concept, which can be introduced in a separate article, but here we can simply and roughly understand that a closure is a function defined inside a function. Closures make variables accessible even if they are outside the scope of the function.

Let's look at an example of using the lambda function as a closure.

Def my_add (n):

... Return lambda x:x+n

...

> add_3 = my_add (3)

> add_3 (7)

ten

The lambda function here is a closure. In the global scope, add_3 (7) can be executed normally and the return value is 10. The reason why it returns 10 is that in the my_add local scope, the value of the variable n in the closure makes it accessible in the global scope.

Closures can also be implemented with regular functions, but it is a bit verbose in this way.

Def my_add (n):

... Def wrapper (x):

... Return Xunn

... Return wrapper

...

> add_5 = my_add (5)

> add_5 (2)

seven

So is it true that lambda functions are clearer than regular functions in any case? Look at this example:

F = lambda x: [[y for j, y in enumerate (set (x)) if (I > > j) & 1] for i in range (2**len (set (x)]

This is a lambda function that returns all subsets of a set. Do you understand? It's hard for me to see it at a glance.

There is a saying in the Zen of Python: Explicit is better than implicit (clarity is better than obscurity) remember, if using lambda functions doesn't make your code clearer, you should consider defining functions in a conventional way.

These are all the contents of the article "how to define functions in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Internet Technology

Wechat

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

12
Report