Python3-Functional programming

Source: Internet
Author: User

function-type programming

Functional programming is a kind of programming method, it treats the computer operation as the function computation; In layman's terms, a mathematical logic is used to define an expression, and then a function is used to implement this function.

Example (python3.0+):
y = 2*x+1  ----first define a mathematical expression def cal (x): "' Description: function to implement the above expression" ' return 2*x+1# the above procedure is functional programming
How can I tell if a function is functional programming?
    • Do not use variables to save state (do not assign values to variables), do not modify variables
Example (python3.0+):
# Non-functional Programming n = 1def cal ():    global n    n+=1    return n cal () # Functional Programming n =1 def CAL (N):    return n+1cal (10)
function is variable

In Python programming, there is a saying: function is variable, how to embody it?

Example (python3.0+):
def foo (n):p rint (n) def Bar (name):    print (' My name is Sunwk ')    foo (bar) # treats the function bar (that is, the memory address of the bar function) as a variable (parameter) Passed to the Foo function result:>>> <function bar at 0x0000000000fa58c8>
Higher order functions

With know more knowledge, contact surface wide, also more and more confused, just began to contact higher-order functions, what is the advanced function Ah, first a few examples to see it

Example (python3.0+):
def foo ():    print (' from foo ') def Bar ():    print (' from bar ')    return FOOA = Bar () print (A ()) # Run result:>>> From Bar>>>from foo>>>none "" "Why did you finally output a None, because print (a ()), first executes a () relative to execute foo (), but after print, the equivalent of output foo ( Return value is not defined in the Foo function, so return none "" "
Summary of higher-order function characteristics:
    1. The parameter received by the function is a functional name
    2. The return value contains the function

Python3-Functional programming

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.