Introduction to Python function programming skills

Source: Internet
Author: User

Python can be regarded as a procedural and object-oriented language, but it contains all the content in the function. The following sections not only discuss the conventional concepts of function programming, it also describes the technical methods for implementing Python functions in Python.

We 'd better start with the most difficult question: "What is functional programming (FP )?" One answer may be that FP is what you do when programming using languages such as Lisp, Scheme, Haskell, ML, OCAML, Clean, Mercury, Erlang, or others. This is a secure answer, but it cannot be clarified very accurately. Unfortunately, it is difficult for function programmers to have a consistent understanding of what FP is.

It seems appropriate to use the story of "blindly touching people" to describe this situation. You can also safely use FP and "command programming" to perform operations such as C, Pascal, C ++, Java, Perl, Awk, TCL, and most other languages, at least to a large extent.

From a personal perspective, I will roughly depict function programming as having at least the following features. A functional language makes these things simple and makes other things difficult or impossible: a function is the first class object ). That is, every operation that can be performed on "data" can be performed using the function itself, for example, passing a function to another function ).

◆ Recursion is used as the main control structure. In some languages, there are no other "loop" structures.
◆ Focus on LISt processing, for example, name Lisp ). Lists are often used together with sublists recursively to replace loops.
◆ "Pure" function language can avoid side effects. This does not include the most common mode in the command language, that is, to specify the first, and then specify the other value to the same variable to track the state of the program.
◆ FP does not encourage or allow statements at all. Instead, it uses expressions to evaluate values. In other words, the function is added with independent variables ). In pure cases, a program is an expression plus supported definitions ).
◆ FP is concerned with computing rather than how to calculate.
◆ Many FP use "Higher-Level" functions. In other words, they operate on some functions, and these functions operate on other functions ).

Function programming advocates believe that all these features lead to faster development of less code and fewer errors. Moreover, senior theoretical scientists in computer science, logic, and mathematics have discovered that the formal performance of function languages and programs is much easier than that of command languages and programs.

The inherent Python function capabilities since Python 1.0, Python has most of the FP features listed above. But for most Python features, they are presented in a very mixed language. Largely because of the OOP feature of Python functions, you can use the desired part and ignore the rest until you need it later ).

Using Python 2.0, the list content adds some great "syntactic whitewashing ". Although the list content does not add any new capabilities, they make many old capabilities look much better. The basic elements of FP in Python are map (), reduce (), filter (), and operator lambda. In Python 1.x, The apply () function is convenient for directly applying the return values of a function list to another function.

Python functions provide improved syntax for this purpose. It may be surprising, but few of these functions and basic operators are enough to write any Python program. In particular, all flow control statements such as if, elif, else, assert, try, break t, finally, for, break, continue, while, and def can be processed in the function style only using FP functions and operators.

Although it is actually useful to eliminate all the flow control commands in the program, it may only be useful to join the "chaotic Python" competition and code that looks like Lisp, however, it is worthwhile to understand how FP uses functions and recursion to represent stream control.

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.