Python Advanced functional Programming (function as parameter)

Source: Internet
Author: User

<title>What is functional programming?</title> What is functional programming?

Functions: function
Functional type: Functional, a programming paradigm

functional programming is a programming mode of abstract computation
function ≠ function, for example: calculate ≠ computer

In the computer, the computer hardware is the lowest level, and the higher the language the more advanced

Low--------------------------------->High
Computer Hardware-->Assembly Language-->C language-->Python language
↓ ↓ ↓
instruction function function Type
Computer------------------------>calculation (mathematics)

0 features of functional programming

    • Treat calculations as functions rather than directives
    • Pure functional Programming: No variables required, no side effects, simple testing
    • Supports higher-order functions with simple code

0 Python-supported functional programming has the following features:

    • Not purely functional programming: allowed variables
    • Supports higher order functions: Functions can also be passed in as variables
    • Support closures: the ability to return functions with closures
    • Support anonymous functions in a limited way
Take a function as a parameter

0 What is a higher order function?

    • Variables can point to functions
      For example, there is a function called abs in Python (for absolute value)
>>> abs(-10)
10
>>> abs
<built-in function abs>
>>> f=abs
>>> f(-20)
20
    • A function name is actually a variable that points to a function
      Same as ABS function
>>>ABS
<built-inch function abs>
>>>ABS=Len
>>>ABS(-Ten)
Traceback (most recent call Last):
File"<stdin>", Line 1,inch<module>
Typeerror:object ofType' int 'Has noLen()
>>>ABS([1,2,3])
3

If we point the ABS variable to another function of the function, then ABS is no longer an absolute function, and this time it will be an error. This time abs points to the Len function. This time we give ABS a list, we can call the normal. There is no difference between a function name and a normal variable, which points to a function name.

    • Higher order functions: functions that can receive function parameters
      • Variables can point to functions
      • Parameters of a function can receive variables
      • A function can receive another function as a parameter
      • Functions that can receive functions as arguments are higher-order functions.

Example: Receiving ABS function

    • Define a function to receive x,y,f three parameters
    • where x, Y is a number, F is a function
>>>def add(x,y,f):
...return f(x)+f(y)
...
>>> add(-5,9,abs)
14

The code above, as defined by the function, actually executes the code:

abs(-5)+abs(9)

Since the parameters x, Y and F can be arbitrarily passed in, if F is passed in to other functions you can get different return values

again for example: calculation
(The square root can be computed with a function: math.sqrt() )

import math

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

print add(25,9,math.sqrt)

Python Advanced functional Programming (function as parameter)

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.