"Python" functional programming

Source: Internet
Author: User

NO1:

Functional programming: That is, a function can be passed as a parameter or as a return value

No2:

map()The function receives two parameters, one is the function, the other is to function the Iterable map incoming function to each element of the sequence sequentially, and the result is returned as a new Iterator

No3:

reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)

No4:

Sort

NO5:

function as return value, each return value function is not the same (should be the object address is not the same)

NO6:

One thing to keep in mind when returning closures: The return function does not refer to any loop variable, or subsequent variable

The reason is that the returned function refers to the loop variable, but it is not executed immediately, so the loop variable is the last value

Solution Solutions

So f (i) will be executed immediately

No7:

anonymous functions

No8:

Decorator a way to dynamically add functionality during code runs, called an "adorner" (Decorator).

@lognow()at the definition of the function, it is equivalent to executing the statement:

now = log(now)

The effect of 3-layer nesting is this:

now = log(‘execute‘)(now)

No9:

The function of functools.partial a partial function is to fix some parameters of a function (that is, to set a default value), to return a new function, to call the new function is simpler

No10:

A. py file is called a module

Each package directory will have a __init__.py file, this file must exist, otherwise, Python will treat this directory as a normal directory, rather than a package. It can be an __init__.py empty file, or it can have Python code, because __init__.py it is a module, and its module name is the package name it contains.

System comes with the SYS module, its own module can not be named sys.py, otherwise it will not be able to import the system's own SYS module

NO11:

module invocation

#!/usr/bin/env Python3#Allows this hello.py file to run directly on the Unix/linux/mac#-*-coding:utf-8-*-'a test module'#the first string of any module code is treated as a document comment for the module__author__='Wang Lei'ImportSYSImportgreetdefTest (): args=SYS.ARGVifLen (args) ==1:        Print('Hello,world')    elifLen (args) ==2:        Print('hello,%s!'% args[1])    Else:        Print('Too many arguments!')        if __name__=='__main__':    #Test ()    Print(Greet.greeting ('WA'))

#!/usr/bin/env Python3#-*-coding:utf-8-*-'a test module'__author__='Wang Lei'def_private_1 (name):return 'hello,%s'%namedef_private_2 (name):return 'hi,%s'%namedefGreeting (name):ifLen (name) >3:        return_private_1 (name)Else:        return_private_2 (name)

Run results

PS d:\wanglei\python> python hello.py
Hi,wa

No12:

Install common modules

Anaclnda Address: https://www.anaconda.com/download/#windows

"Python" 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.