Some advanced features of Python functions

Source: Internet
Author: User
Tags builtin
This article mainly introduces some advanced features of Python functions, including tips on using function names as variables. For more information, see Higher-order function. What is a high-level function? Let's take the actual code as an example and go into the concept step by step.
Variable can point to function

Take the abs () function built in Python for absolute value calculation as an example. to call this function, use the following code:

>>> abs(-10)10

But what if I only write abs?

>>> abs
 

It can be seen that abs (-10) is a function call, while abs is the function itself.

To obtain the function call result, we can assign the result to the variable:

>>> x = abs(-10)>>> x10

But what if I assign the function itself to the variable?

>>> f = abs>>> f
 

Conclusion: the function itself can also be assigned a value to the variable, that is, the variable can point to the function.

If a variable points to a function, can this function be called through this variable? Verify with code:

>>> f = abs>>> f(-10)10

Successful! It indicates that the variable f has now pointed to the abs function itself.
The function name is also a variable.

So what is the function name? The function name is actually a variable pointing to the function! For the abs () function, you can regard the function name abs as a variable, which points to a function that can calculate the absolute value!

What happens if abs points to another object?

>>> abs = 10>>> abs(-10)Traceback (most recent call last): File "
 
  ", line 1, in 
  
   TypeError: 'int' object is not callable
  
 

After pointing abs to 10, you cannot call this function through abs (-10! The abs variable does not point to the absolute value function!

Of course, the actual code cannot be written like this. this is to show that the function name is also a variable. To restore the abs function, restart the Python interaction environment.

Note: Because the abs function is actually defined in the _ builtin _ module, to make the point of modifying the abs variable take effect in other modules, use _ builtin __. abs = 10.
Input function

Since variables can point to functions and function parameters can receive variables, one function can receive another function as a parameter, which is called a higher-order function.

One of the simplest high-level functions:

def add(x, y, f):  return f(x) + f(y)

When we call add (-5, 6, abs), parameters x, y, and f receive-5, 6, and abs respectively. according to the function definition, we can deduce the calculation process as follows:

x ==> -5y ==> 6f ==> absf(x) + f(y) ==> abs(-5) + abs(6) ==> 11

Verify with code:

>>> add(-5, 6, abs)11

Writing a high-order function allows the function parameters to receive other functions.
Summary

Passing in a function as a parameter is called a high-order function. function programming is a highly abstract programming paradigm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.