Python functional programming + small feeling at the end of the year

Source: Internet
Author: User

Since I was an elementary school teacher, I taught two programming languages: process-oriented and object-oriented. For example, the same elementary school computer class faces win95. It seems that windows is the alias of the operating system, the computer centers in colleges and universities are all windows, so linux should be started on its own, and functional programming should also be started on its own ~ Not for credits, not for scholarship, not for salary, but just follow your own interests. This is the happiest thing ~

Python has a major advantage, that is, it can be either object-oriented or process-oriented or function-oriented. Therefore, to experience functional programming, you don't need to change to erlangh or scheme. Features of functional programming:

1. Variable immutable;

2. closure;


According to the features of functional programming, a function only accepts input and generates output, and does not change the value of external variables or use the value of external variables. Because of this, functional programming only requires one thing for a function, which facilitates modularization and requires less code.


1. lambda

Lambda is an anonymous function with no function name. It is useful in closures and map reduce. The following is a function that calculates the product of two numbers:

>>>func=lambda x,y:x*y>>>func(3,4)12

2. map

The map function is used to execute functions in sequence for each item in a sequence. The following is to multiply each item in a sequence by 2:

>>> a=map(lambda x:x*2,[1,2,3])>>> list(a)[2, 4, 6]

3. reduce

The reduce function calls the function for each iteration of a sequence. The factorial of 3 is as follows:

>>> functools.reduce(lambda x,y:x*y,range(1,4))6

Well, this is a few of the functions commonly used in python functional programming. Now we use an example to evaluate the factorial of n to compare the differences between functional programming and process-oriented programming:

>>> def func(n):...     if n==1:...             return 1...     return n*func(n-1)... >>> func(4)24>>> g=lambda n:functools.reduce(lambda x,y:x*y,range(1,n+1))>>> g(4)24

Very concise and clean ~ Then let's look at the closure of functional programming:

>>> def funA():...     x=0...     def funB():...             nonlocal x...             x+=1...             print(x)...     return funB... >>> a=funA()>>> a()1>>> a()2>>> a()3

A closure is a defined function in a function. The function can use variables in an external scope, but not global variables. Therefore, an external function is created at a time, when the internal function is called multiple times, the variables in the external scope are still valid. When the external function is run multiple times, the closure is re-created, and the value of the original external variable is not affected, in the preceding example, the external variable + 1 operation can be performed each time an internal function is called. Here we will talk about the scope. The original python2 only has a global scope and a local scope. The new nonlocal scope of python3 is specifically for closures. The variables declared by nonlocal can only access variables in the external scope, you cannot access variables in the global scope. In this way, you do not need to make global variables to make any mistakes and you do not know where the errors occur.

After writing this article, I wish myself and everyone a happy new year at the end of 2013 and live for your interest ~ Haha


Reprinted Please note:

Related Article

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.